而不是使用模擬,也許你的情況會更好地使用Stub。
public class StubDataProvider : IDataProvider
{
public T GetDataDocument<T>(Guid document) where T : class, new()
{
return new T();
}
}
如果你真的需要一個模擬(這樣你就可以驗證GetDataDocument
叫)。與其試圖與Mocking框架搏鬥,有時候更容易創建一個Mock類。
public class MockDataProvider : IDataProvider
{
private readonly Action _action;
public MockDataProvider(Action action)
{
_action = action;
}
public T GetDataDocument<T>(Guid document) where T : class, new()
{
_action();
return new T();
}
}
,比你的測試:
bool wasCalled = false;
IDataProvider dataProvider = new MockDataProvider(() => { wasCalled = true; });
var aTable = dataProvider.GetDataDocument<ATable>(new Guid());
Debug.Assert(wasCalled);
好解決方案,儘管我希望看到一些模擬/存根框架可以自動執行此操作:)我會盡量等待一會,也許另一個答案會提示auotomatic解決方案。 – Hassan 2011-03-15 12:29:43