2012-01-26 20 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

あなたが本当にここモックを使用する必要はありません。

あなたSUTWorkstationLocatorクラスのように見える、あなたがチェックし、すべては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は、典型的には、特定のメソッドが呼び出されたことを確認するために使用されます。あなたがそのx.Path == workstationInstallationPathをテストしている場合たとえば、

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

は、あなたが本当にただのメソッド呼び出しのいくつかの並べ替えによって設定されたいずれかであることを確認していない、両方の値が同じであることを主張しています。