我測試我的課在嘲笑對象通過驗證設置
public class myclass
{
private IAwesome awesomeObject;
public myclass(IAwesome awesomeObject)
{
this.awesomeObject = awesomeObject;
}
public void MethodUnderTest()
{
this.awesomeObject.RunSomething(); //I want to verify that RunSomething was called
}
}
我這樣做的方法是:
//Arrange
var mockAwesome = new Mock<IAwesome>();
mockAwesome.Setup(x=>x.RunSomething()).Returns ... Verify()...;
//Act
var sut = new myclass(mockAwesome.object);
sut.MethodUnderTest();
//Assert
mockAwesome.Verify();
我得到的例外是:
System.NotSupportedException:表達式引用 不屬於模擬對象的方法:x => x.RunSomething
難道不可能測試一個特定的方法是在我傳入一個類的模擬對象上執行的,它現在是該類的私有成員的一部分嗎?
將設置行修改爲'mockAwesome.Setup(x => x.RunSomething())。Verifiable()',它應該適用於您提供的示例。要確認,請在示例類中註釋掉'this.awesomeObject.RunSomething()'並運行測試。它會失敗,因爲你將'RunSomething'設置爲'Verifiable()'並且它沒有被使用。 – Nkosi