設定的功能被定義爲一個屬性,所以你應該在起訂量
public interface IPersona
{
string nome { get; set; }
string cognome { get; set; }
Func<Owned<ISomeInterface>> somemethod { get; set; }
}
使用SetupSet。在您的測試:
您創建的函數功能一個模擬:
Func<Owned<ISomeInterface>> somemethodMock =() => new Mock<Owned<ISomeInterface>>().Object;
那麼你設置的模擬包含了函數功能作爲一個屬性,你設置的設置方法的預期類:
var obj = new Mock<IMyInterface>();
obj.SetupSet(x => x.somemethod = somemethodMock).Verifiable();
您創建模擬容器對象:
//We pass the mocked object to the constructor of the container class
var container = new Container(obj.Object);
container.AnotherMethod(somemethodMock);
obj.VerifyAll();
這裏是d Container類的另一種方法的efinition,如果得到FUNC作爲輸入參數,並將其設置爲
enter public class Container
{
private IPersona _persona;
public Container(IPersona persona)
{
_persona = persona;
}
public void AnotherMethod(Func<MyClass<IMyInterface>> myFunc)
{
_persona.somemethod = myFunc;
}
}
我已經閱讀計算器 – 2012-01-06 09:27:53
一個相關的帖子難道說這個問題應該被關閉所包含的對象的屬性作爲重複?請澄清,如果這個問題已經解決.. – MattDavey 2012-01-06 09:33:16
對不起,這篇文章似乎沒有幫助我,因爲它不適合我。我的意思是編輯問題,但意外添加了評論。我現在更新了這個問題 – 2012-01-06 09:35:07