ViqMontana
ViqMontana

Reputation: 5698

How to mock a property using NSubstitute while using a real implementation

I have some NHibernate repositories and I would like my SpecFlow tests to cover them.

I have a repository for Staff like so:

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

Where Repository is a property that lives on the base type - this is the property I would like to mock. I am using structure map to inject all my classes, and then mocking the StaffRepository like so:

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

My problem is, when I mock the Repository property like so:

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

I always receive the following error message:

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

What am I doing wrong here?

Upvotes: 0

Views: 1049

Answers (1)

ViqMontana
ViqMontana

Reputation: 5698

I figured it out in the end. The Repository must be virtual or abstract; changing this to virtual resolved the issue.

Upvotes: 1

Related Questions