如何在使用实际实现时使用 NSubstitute 模拟属性



我有一些NHibernate存储库,我希望我的SpecFlow测试能够涵盖它们。

我有一个这样的员工存储库:

public class StaffRepository : NHibernateRepository<IStaff>, 
{
public IEnumerable<IStaff> GetByStaffId(string staffId)
{
return Repository.Where(ab => ab.StaffId == staffId);
}
}

其中Repository是位于基本类型的属性 - 这是我想模拟的属性。我正在使用结构图来注入我所有的类,然后像这样模拟 StaffRepository:

pmsRepository = Substitute.For<StaffRepository>();
ApplicationContext.Register<IStaffRepository, StaffRepository>(pmsRepository);

我的问题是,当我像这样模拟Repository属性时:

pmsRepository.Query.Returns(ListOfStaffes.AsQueryable());

我总是收到以下错误消息:

NSubstitute.Exceptions.CouldNotSetReturnDueToNoLastCallException: 'Could not find a call to return from.

我在这里做错了什么?

我最终想通了。Repository必须是虚拟的或抽象的;将其更改为虚拟解决了问题。

最新更新