我是 Moq 和一般测试的新手,所以这是我的菜鸟问题。我如何测试请求上的Status属性是否已使用 Moq 设置?
public class DudeManager
{
private readonly IDRepository _repo;
public DManager(IDRepository repo)
{
_repo = repo;
}
public void Create(Request r)
{
r.Status = Status.Submitted;
_repo.AddRequest(r);
}
}
有比以下更好的方法吗?也许使用验证集?
[TestMethod]
public void AddingNewRequestSetsStatusToSubmitted()
{
//Arrange
var mock = new Mock<IDRepository>();
var mockRequest = new Mock<Request>();
var dManager = new DManager(mock.Object);
//Act
dManager.Create(mockRequest.Object);
//Assert
Assert.AreEqual(Status.Submitted, mockRequest.Object.Status);
}
编辑:这是我在所有有用的建议之后最终使用的方法:
//Arrange
var request = new Request();
var mock = new Mock<IDRepository>();
var dManager = new DManager(mock.Object);
mock.Setup(x => x.AddRequest(It.IsAny<Request>()));
//Act
dManager.QueueNewRequest(request);
//Assert
Assert.AreEqual(RequestStatus.Submitted, request.Status);
这种方法对我来说似乎是正确的。有人不这么认为吗?