2017-08-14 90 views
0

我有一些NHibernate存储库,我希望我的SpecFlow测试覆盖它们。如何使用NSubstitute在使用实际实现时模拟属性

我有工作人员信息库,像这样:

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. 

我在做什么错在这里?

回答

0

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