Rhino Mocks – 测试存储库层返回“对象引用未设置为实例”错误

谨慎的是,我首先说我是犀牛模拟的新手,而且更普遍地嘲笑。

考虑到这一点,我正在尝试对我的Linq to SQL存储库层进行unit testing,以确保正在命中datacontext上的正确方法,并确保LINQ to SQL正确过滤。

〜为了清晰起见〜

有问题的方法 – ‘GetRecordWhere’ – 在Repository类中定义。 它在DataContextWrapper上调用方法 – ‘GetTable’ – 它是我的Linq to SQL DataContext(自动生成)的自定义包装器,它被实现以使DataContext可模拟。

public interface IDataContextWrapper : IDisposable { IQueryable GetTable() where TName : class; } public class DataContextWrapper : IDataContextWrapper { public IQueryable GetTable() where TName : class { return _db.GetTable().AsQueryable(); } } public class Repository : IRepository { public T GetRecordWhere(Expression<Func> predicate) where T : class { return _dataContext.GetTable().Where(predicate).SingleOrDefault(); } } 

当我尝试存根’GetTable’方法以提供可查询的结果集时,抛出我当前遇到的错误,该结果集可以使用’GetRecordWhere’方法进行查询。

ArgumentNullExcpetion:值不能为null。 抛出参考线:

 Arg<Expression<Func>>.Is.Anything 

..我也尝试过Is.NotNull和一个特定的谓词。

unit testing示例:

  _dataContext = MockRepository.GenerateMock(); [Test] public void GetRecordWhere() { // Arrange var billing = new Billing { BillingId = 1 }; var billingQueryList = new List {billing}; const int testId = 1; _dataContext.Stub(x => x.GetTable() .Where(Arg<Expression<Func>>.Is.Anything) .SingleOrDefault()) .Return(billing); _intRepository = new Repository(_dataContext); // Act var result = _intRepository.GetRecordWhere(x => x.BillingId == testId); // Assert Assert.IsNotNull(result); Assert.AreEqual(result.BillingId, testId); _dataContext.AssertWasCalled(x => x.GetTable()); } 

这是我对RhinoMocks的理解失败吗?

感谢帮助!

任何想要使用Rhino.Mocks进行模拟的方法都需要是虚拟的,因此Rhino.Mocks可以拦截它并提供您定义的存根/模拟行为。 看看你对GetTable的定义,它不是虚拟的,因此不能被模拟。

更新:

不要“链接”你的方法模拟。 只需定义您希望方法执行的操作并返回值:

 _dataContext.Stub(x => x.GetTable()).Return(billingQueryList.AsQueryable()); 

我只是将您的示例代码插入到unit testing中,并使用上面的存根设置,测试通过。