我试图使用 rhino 模拟在 C# 中存根以下代码行,尽管没有成功。请问有什么建议吗?这是导致测试失败的行
var header = this.repository.Headers.FirstOrDefault(h => h.Id == id);
详情如下,非常感谢!
单元测试
private IRepository _repository;
[TestInitialize]
public void SetUp()
{
_repository = _mockRepository.Stub<IRepository>();
_commandService = new CoreCommandService(_repository);
}
[TestMethod]
public void MyTest()
{
// Line that doesn't work
_repository.Stub(x => x.Headers).Return(SomeThing);
}
执行
// The Headers is stubbed although Id is null
var header = this.repository.Headers.FirstOrDefault(h => h.Id == id);
public interface IRepository
{
IEntityRepository<Header> Headers { get; }
}
更新#1
public interface IEntityRepository<TEntity> : IQueryable<TEntity>, IDisposable where TEntity : class
更新#2
使用以下示例
var wrapper = new HeadersWrapper(...);
_repository.Stub(x => x.Headers).Return(wrapper);
编译时返回以下信息
HeaderWrapper is not assignable to parameter type IEntityRepository<Header>