如何在使用真实实现时使用 NSubstitute 模拟 属性
How to mock a property using NSubstitute while using a real implementation
我有一些 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
必须是虚拟的或抽象的;将其更改为虚拟解决了问题。
我有一些 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
必须是虚拟的或抽象的;将其更改为虚拟解决了问题。