Mock for Get 方法在 mstest 中不起作用
Mock for Get method not working in mstest
我有一家名为 EmployeeService 的公司 class,它有一个方法 GetProjectsByEmpId,代码如下
public List<Project> GetProjectsByEmpId (int id) {
return this.empRepository.Get(e=>e.empId == id, e=>e.Projects).Projects;
}
并且 Get 方法语法是
T Get(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes);
我的测试用例是
private Mock<IEmployeeRepository> mockEmpRep;
[TestInitialize]
public void setup()
{
mockEmpRep= new Mock<IEmployeeRepository>();
myClass= new EmployeeService(mockEmpRep.Object);
}
测试方法是
[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);
}
mockResult 是
new Employee { id: 1, projects: List<Project>()}
我正在尝试模拟所有可能的值,但出现 "Parameter count mismatch." 错误。
如果我在这里做错了什么,你能告诉我吗?
感谢您的帮助并提前致谢。
这是我的完整工作示例。您能否验证并告诉我我的解决方案与您的代码之间的区别?
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);
}
}
}
这是我 运行 在本地使用的代码版本,可以确认它是否有效。
public class EmployeeService
{
private readonly IEmployeeRepository empRepository;
public EmployeeService(IEmployeeRepository empRepository)
{
this.empRepository = empRepository;
}
public List<Project> GetProjectsByEmpId(int id)
{
return this.empRepository.Get<Employee>(e => e.empId == id, e => e.Projects).Projects;
}
}
public class Employee
{
public int empId { get; set; }
public List<Project> Projects { get; set; }
}
public class Project
{
}
public interface IEmployeeRepository
{
T Get<T>(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes);
}
[TestClass]
public class EmployeeServiceTests
{
private Mock<IEmployeeRepository> mockEmpRep;
private EmployeeService myClass;
[TestInitialize]
public void setup()
{
mockEmpRep = new Mock<IEmployeeRepository>();
myClass = new EmployeeService(mockEmpRep.Object);
}
[TestMethod]
public void Get()
{
// Arrange
var mockResult = new Employee()
{
empId = 1,
Projects = new List<Project>()
{
new Project()
}
};
mockEmpRep.Setup(m =>
m.Get(It.IsAny<Expression<Func<Employee, bool>>>(),
It.IsAny<Expression<Func<Employee, object>>>())).Returns(mockResult);
// Act
var result = myClass.GetProjectsByEmpId(1);
// Assert
Assert.AreEqual(1, result.Count);
}
}
我有一家名为 EmployeeService 的公司 class,它有一个方法 GetProjectsByEmpId,代码如下
public List<Project> GetProjectsByEmpId (int id) {
return this.empRepository.Get(e=>e.empId == id, e=>e.Projects).Projects;
}
并且 Get 方法语法是
T Get(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes);
我的测试用例是
private Mock<IEmployeeRepository> mockEmpRep;
[TestInitialize]
public void setup()
{
mockEmpRep= new Mock<IEmployeeRepository>();
myClass= new EmployeeService(mockEmpRep.Object);
}
测试方法是
[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);
}
mockResult 是
new Employee { id: 1, projects: List<Project>()}
我正在尝试模拟所有可能的值,但出现 "Parameter count mismatch." 错误。
如果我在这里做错了什么,你能告诉我吗? 感谢您的帮助并提前致谢。
这是我的完整工作示例。您能否验证并告诉我我的解决方案与您的代码之间的区别?
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);
}
}
}
这是我 运行 在本地使用的代码版本,可以确认它是否有效。
public class EmployeeService
{
private readonly IEmployeeRepository empRepository;
public EmployeeService(IEmployeeRepository empRepository)
{
this.empRepository = empRepository;
}
public List<Project> GetProjectsByEmpId(int id)
{
return this.empRepository.Get<Employee>(e => e.empId == id, e => e.Projects).Projects;
}
}
public class Employee
{
public int empId { get; set; }
public List<Project> Projects { get; set; }
}
public class Project
{
}
public interface IEmployeeRepository
{
T Get<T>(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes);
}
[TestClass]
public class EmployeeServiceTests
{
private Mock<IEmployeeRepository> mockEmpRep;
private EmployeeService myClass;
[TestInitialize]
public void setup()
{
mockEmpRep = new Mock<IEmployeeRepository>();
myClass = new EmployeeService(mockEmpRep.Object);
}
[TestMethod]
public void Get()
{
// Arrange
var mockResult = new Employee()
{
empId = 1,
Projects = new List<Project>()
{
new Project()
}
};
mockEmpRep.Setup(m =>
m.Get(It.IsAny<Expression<Func<Employee, bool>>>(),
It.IsAny<Expression<Func<Employee, object>>>())).Returns(mockResult);
// Act
var result = myClass.GetProjectsByEmpId(1);
// Assert
Assert.AreEqual(1, result.Count);
}
}