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.
InCode
  • 503
  • 3
  • 8
  • 25
  • 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 '14 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 '14 at 13:46

2 Answers2

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));
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • 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 '14 at 14:05
  • `AsQueryable` is just a [LINQ extension method](http://msdn.microsoft.com/en-us/library/vstudio/bb909469(v=vs.90).aspx). Depending on the actual signature of `Find()`, you might be able to leave it off entirely. [More here](http://stackoverflow.com/q/17366907/314291) – StuartLC Apr 24 '14 at 14:09
  • What is SUT? Service Under Test? – InCode Apr 24 '14 at 14:17
  • It's `List.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 '14 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 '14 at 18:01
  • @mart - no particular reason - I'm sure its just DSL sugar - in fact, I use `Times.Never` (vs `Times.Exactly(0)`) all the time. Just style I guess :) – StuartLC Apr 24 '14 at 19:03
0

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

Jim Bolla
  • 8,265
  • 36
  • 54