我有一个Employee类,其中一个方法未实现
namespace Employee
{
public class Employee
{
public virtual DateTime getDateOfJoining(int id)
{
throw new NotImplementedException();
}
}
}
我想模仿这个,我期待该方法返回当前的DateTime。
namespace Employee.Tests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void getJoiningDateMock()
{
var employee = new Mock<Employee>();
employee.Setup(x => x.getDateOfJoining(It.IsAny<int>())).Returns((int x) => DateTime.Now);
var objEmp = new Employee();
Assert.AreEqual(DateTime.Now, employee.getDateOfJoining(1));
}
}
}
我调用Mock的方式不正确。我在这里缺少什么,特别是如何编写AssertEquals?
答案 0 :(得分:1)
以下仅确认模拟的行为与设置相同。
[TestClass]
public class UnitTest1 {
[TestMethod]
public void getJoiningDateMock() {
//Arrange
var expected = DateTime.Now;
var employeeMock = new Mock<Employee>();
employeeMock
.Setup(x => x.getDateOfJoining(It.IsAny<int>()))
.Returns(expected);
var objEmp = employeeMock.Object;
//Act
var actual = objEmp.getDateOfJoining(1);
//Assert
Assert.AreEqual(expected, actual);
}
}