Search code examples
c#entity-frameworkmockingmstest.net-framework-version

Mock for Get method not working in mstest


I have a business class called EmployeeService and it is having one method GetProjectsByEmpId and code follows

    public List<Project> GetProjectsByEmpId (int id) {
       return this.empRepository.Get(e=>e.empId == id, e=>e.Projects).Projects;
   }

and Get method synatx is

T Get(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes);

and my testcase is

private Mock<IEmployeeRepository> mockEmpRep;
 [TestInitialize]
        public void setup()
        {
            mockEmpRep= new Mock<IEmployeeRepository>();
            myClass= new EmployeeService(mockEmpRep.Object);            
        }

and test method is

 [TestMethod]
    public void Test_GetProjectsByEmpId() {
    this.mockEmpRep.Setup(m=> m.Get(
    It.IsAny<Expression<Func<Employee, bool>>>(),
                    It.IsAny<Expression<Func<Employee, object>>>())).Returns(mockResult);
    var res = myClass.GetProjectsByEmpId(1);
    }

and mockResult is

new Employee { id: 1, projects: List<Project>()}

I am trying to mock for all possible values but I am getting "Parameter count mismatch." error.

Can you please tell me if I am doing anything wrong here. Appreciate your help and thanks in advance.


Solution

  • Here is my full working example. Could you verify and tell me a difference between my solution and your code?

    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Moq;
    using System;
    using System.Collections.Generic;
    using System.Linq.Expressions;
    
    namespace UnitTestProject1
    {
        public class Project { }
    
        public class Employee
        {
            public int EmpId { get; set; }
            public List<Project> Projects { get; set; }
        }
    
        public class EmployeeService
        {
            readonly IEmployeeRepository<Employee> empRepository;
    
            public EmployeeService(IEmployeeRepository<Employee> repository)
            {
                empRepository = repository;
            }
    
            public List<Project> GetProjectsByEmpId(int id)
            {
                return empRepository.Get(e => e.EmpId == id, e => e.Projects).Projects;
            }
        }
    
        public interface IEmployeeRepository<T>
        {
            T Get(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes);
        }
    
        [TestClass]
        public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
                var mockEmpRep = new Mock<IEmployeeRepository<Employee>>();
                var mockResult = new Employee() { Projects = new List<Project> { new Project() } };
    
                mockEmpRep.Setup(
                    x => x.Get(
                        It.IsAny<Expression<Func<Employee, bool>>>(),
                        It.IsAny<Expression<Func<Employee, object>>>())
                    ).Returns(mockResult);
    
                EmployeeService myClass = new EmployeeService(mockEmpRep.Object);
                List<Project> result = myClass.GetProjectsByEmpId(1);
    
                Assert.AreEqual(1, result.Count);
            }
        }
    }