1

Using MOQ and a Generic Repository. I'm not able to successfully test the following method.

[TestMethod()]
    public void Employee_Service_Get_Users()
    {

        var mockRep = new Mock<IRepository<Employee>>(MockBehavior.Strict);

        IList<Employee> newEmpLst = new List<Employee>();
        Employee newEmp = new Employee();            

        mockRep.Setup(repos => repos.Find()   <------ What belongs here?

        var service = new EmployeeService(mockRep.Object);
        var createResult = service.GetAllActiveEmployees();

        Assert.AreEqual(newEmpLst, createResult);

    }

It's calling this Method:

public IList<Employee> GetAllActiveEmployees()
    {
        return _employeeRepository.Find()
               .Where(i=>(i.Status =="Active")).ToList();  <----It Bombs Here! ;)
    }

My Generic Repository has the following:

 public IQueryable<T> Find()
    {
        var table = this.LookupTableFor(typeof(T));
        return table.Cast<T>();
    }

I get the following:

Moq.MockException: IRepository`1.Find() invocation failed with 
mock  behavior Strict. All invocations on the mock must have a corresponding 
setup.
2
  • You've setup a repos.FindAll whereas the invoked dependency is _employeeRepository.Find(). You'll need to add a Setup for Find()
    – StuartLC
    Apr 24, 2014 at 13:40
  • Sorry that was edited. I'm just not sure how we call find() for that function. I don't know what is needed.
    – InCode
    Apr 24, 2014 at 13:46

2 Answers 2

3

You haven't provided the full method signature of IRepository<T> Find(), but at a guess, it is something like IQueryable<T> Find(). Because we want to mock it to return a small amount of fake data, it doesn't really matter that we just bind it to an in memory List. Because the SUT performs a filter (Active), ensure that you also provide unwanted data in the fake data to ensure that the SUT's filtering logic is working correctly.

Assuming all of this, your setup would look something like:

 var newEmpLst = new List<Employee>
 {
    new Employee()
    {
        Name = "Jones",
        Status = "Active"
    },
    new Employee()
    {
        Name = "Smith",
        Status = "Inactive"
    },
 };

mockRep.Setup(repos => repos.Find())
       .Returns(newEmpLst.AsQueryable());

Your Act + Assert would then be something like:

var service = new EmployeeService(mockRep.Object);
var createResult = service.GetAllActiveEmployees();

Assert.AreEqual(1, createResult);
Assert.IsTrue(createResult.Any(x => x.Name == "Jones"));
Assert.IsFalse(createResult.Any(x => x.Name == "Smith"));

mockRep.Verify(repos => repos.Find(), Times.Exactly(1));
6
  • Ok this makes sense. I added the Find() on the Repository. I don't have .AsQueryable I'm using Moq 4.2.1402.2112
    – InCode
    Apr 24, 2014 at 14:05
  • AsQueryable is just a LINQ extension method. Depending on the actual signature of Find(), you might be able to leave it off entirely. More here
    – StuartLC
    Apr 24, 2014 at 14:09
  • What is SUT? Service Under Test?
    – InCode
    Apr 24, 2014 at 14:17
  • It's List<T>.AsQueryable() - ensure that you have a using System.Linq; at the top of your test. SUT = System Under Test - basically the class / method / whatever it is that you are actually testing (as opposed to the dependencies that you are mocking / faking) :)
    – StuartLC
    Apr 24, 2014 at 14:27
  • Why you are using Times.Exactly(1) over Times.Onces? Is there any impreovement in the performance or is just a style rule you have adopted?
    – mart
    Apr 24, 2014 at 18:01
0

You set up an expectation for FindAll but then called Find.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.