我有一个类、一个服务和两个接口:
public class MyBasicObject
{
public MyBasicObject() { }
public int Id { get; set; }
public string Name { get; set; }
}
public interface ICacheProvider
{
T Get<T>(string key, Func<T> fetcher) where T:class;
}
public interface IMyBasicObjectRepository
{
MyBasicObject GetByName(string name);
}
public class MyBasicObjectService
{
public MyBasicObjectService(ICacheProvider cacheProvider,
IMyBasicObjectRepository repository)
{
CacheProvider = cacheProvider;
MyBasicObjectRepository = repository;
}
public ICacheProvider CacheProvider { get; set; }
public IMyBasicObjectRepository MyBasicObjectRepository { get; set; }
public MyBasicObject GetByName(string name)
{
return CacheProvider.Get<MyBasicObject>(name, () =>
MyBasicObjectRepository.GetByName(name));
}
}
使用 RhinoMocks,我希望能够验证何时MyBasicObjectService.GetByName("AnUniqueName")
执行,CacheProvider.Get("AnUniqueName", () => MyBasicObjectRepository.GetByName("AnUniqueName"))
. 我有一个这样的固定装置:
[TestFixture]
public class MyBasicObjectServiceFixture
{
[Test]
public void GetByNameShouldCallCacheProviderFunction()
{
// Arrange
MockRepository mock = new MockRepository();
IMyBasicObjectRepository repo = mock.DynamicMock<IMyBasicObjectRepository>();
ICacheProvider cacheProvider = mock.DynamicMock<ICacheProvider>();
MyBasicObjectService service = new MyBasicObjectService(cacheProvider, repo);
cacheProvider.Expect(p => p.Get<MyBasicObject>("AnUniqueName", () => repo.GetByName("AnUniqueName")));
mock.ReplayAll();
// Act
var result = service.GetByName("AnUniqueName");
// Assert
mock.VerifyAll();
}
}
我希望这个测试能够通过,但是在运行时,断言失败,通知我cacheProvider.Expect
没有调用其中布局的函数。我错过了什么注册吗?模拟和测试采用 Func<> 参数的方法?
编辑:
所以如果我这样做:
cacheProvider.Expect(p => p.Get<MyBasicObject>("AnUniqueName", () => repo.GetByName("AnUniqueName"))).IgnoreArguments();
(也就是说,将 IgnoreArguments() 方法添加到 expect 调用的末尾)
...测试通过就好了。那么,我假设传入的参数有问题。在调用缓存提供程序方法的期望中,我做错了什么,但它阻塞了传入的匿名方法?