2012-01-26 73 views
1

好日,Moq的驗證表達

我具有執行註冊表查找以確定在那裏的應用程序被安裝(在64位機器上)的類。

我試圖驗證,在這裏寫單元測試是我所:

[Test, Explicit] 
public void Validate64Bit() 
{ 
    wsMock.Setup(x => x.IsInstalled).Returns(true); 
    wsMock.Setup(x => x.Path).Returns(@"C:\Program Files (x86)\DIRP\"); 

    IWorkstationLocator workstationLocator = new WorkstationLocator(); 
    string workstationInstallationPath = workstationLocator.Path; 

    Assert.That(workstationInstallationPath != string.Empty, "The install path should exist."); 
    wsMock.Verify(x => x.Path == workstationInstallationPath, 
     "64-bit Workstation Install Path should match: " + @"C:\Program Files (x86)\DIRP\"); 
    } 

但我發現了一個錯誤:

System.ArgumentException:表達式是不是方法調用: x => x.Path == .workstationInstallationPath

所以我的問題是:我想測試如果x.Path == wrokstationInstallationPath。

我該如何在.Verify()方法中執行此操作?

或者我最好使用斷言?

TIA,

COSON

回答

2

你並不真的需要在這裏使用一個模擬。

SUT似乎是WorkstationLocator類和所有你檢查的是,Path屬性等於一個特定的值。

你可以簡單地做:

[Test, Explicit] 
public void Validate64Bit() 
{ 
    var expectedPath = @"C:\Program Files (x86)\DIRP\"; 

    IWorkstationLocator workstationLocator = new WorkstationLocator(); 

    Assert.AreEqual(expectedPath, workstationLocator.Path, 
     "64-bit Workstation Install Path should match: " + expectedPath); 
} 
2

起訂量的Verify通常用來驗證特定方法被調用。例如,

// Verify with custom error message for failure 
mock.Verify(foo => foo.Execute("ping"), "When doing operation X, the service should be pinged always"); 

如果你正在測試一個x.Path == workstationInstallationPath,你真的只是聲稱兩個值是相同的,沒有核實,要麼是通過某種方法調用的設置。