有一個功能:如何用Moq測試lambda函數?
public class MyCacheClass : ICache
{
public void T GetObject<T>(Func<T> func)
{
T t;
...
t = func();
...
return t;
}
}
public class MyWorkClass : IWork
{
public Object MyWorkMethod(string value)
{
return new object();
}
}
這些功能被稱爲以下列方式:
public class MyTestableClass
{
public void MyTestableFunc(ICache cache, IWorkClass work)
{
string strVal="...";
...
Object obj = cache(()=>work.MyWorkMethod(strVal));
...
}
}
有必要寫單元測試(有MOQ)爲並檢查是否適當的參數作爲傳入'MaCacheClass.GetObject'。
應該是這樣的:
[TestMethod]
public void MyTest()
{
Mock<ICache> mockCache = new Mock<ICache>();
Mock<IWorkClass> mockWorkClass = new Mock<IWorkClass>();
MyTestableClass testable = new MyTestableClass();
testable.MyTestableFunc(mockCache.Object, mockWorkClass.Object);
// should I check if 'MyCacheClass' was called with proper parameter?
mockCache.Verify(mock=>mock.GetObject(...)).Times.Once());
}
我怎麼可能提供的參數,將適合作爲「拉姆達功能」? 還有其他的選擇嗎?
歡迎任何想法。