C# 检查是否设置了属性 - 使用 Moq
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16926995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a property was set - using Moq
提问by mithun_daa
I am new to Moq and testing in general so here is my noobish Q. How do I test if the Statusproperty on Requesthas been set using Moq?
我是 Moq 的新手并进行一般测试,所以这是我的菜鸟问题。如何测试Request上的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);
}
}
Is there a better approach than the following? Maybe using VerifySet?
有没有比以下更好的方法?也许使用验证集?
[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);
}
EDIT:This is the approach I ended up using after all the helpful suggestions:
编辑:这是我在所有有用的建议之后最终使用的方法:
//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);
This approach seems right to me. Does anyone think otherwise?
这种方法对我来说似乎是正确的。有人不这么认为吗?
采纳答案by neontapir
I think VerifySet is the right approach. It would look something like this:
我认为 VerifySet 是正确的方法。它看起来像这样:
//Arrange
var mock = new Mock<IDRepository>();
var mockRequest = new Mock<Request>();
// TODO: set some expectations here
var dManager = new DManager(mock.Object);
//Act
dManager.Create(mockRequest.Object);
//Assert
mockRequest.VerifySet(x => x.Status = Status.Submitted);
I believe in your case, it blows up because you haven't set up your Request mock to handle the set operation on Status.
我相信在你的情况下,它会爆炸,因为你没有设置你的请求模拟来处理状态上的设置操作。
One easy way to do that is using SetupAllProperties, like so:
一种简单的方法是使用SetupAllProperties,如下所示:
//Arrange
var mock = new Mock<IDRepository>();
var mockRequest = new Mock<Request>();
mockRequest.SetupAllProperties();
回答by Ufuk Hac?o?ullar?
I think you should use strict behavior by default, then you can make the verification with a single call. It also makes you write your test more explicitly.
我认为您应该默认使用严格行为,然后您可以通过一次调用进行验证。它还使您更明确地编写测试。
[TestMethod]
public void AddingNewRequestSetsStatusToSubmitted()
{
//Arrange
var mock = new Mock<IDRepository>(MockBehavior.Strict);
var mockRequest = new Mock<Request>(MockBehavior.Strict);
var dManager = new DManager(mock.Object);
mockRequest.SetupSet(item => item.Status = It.IsAny<StatusType>())
.Verifiable();
//Act
dManager.Create(mockRequest.Object);
//Assert
Assert.AreEqual(mockRequest.Object.Status, Status.Submitted);
mock.VerifyAll();
mockRequest.VerifyAll();
}
回答by AD.Net
mock.Verify(m=>m.AddRequest(It.Is<Request>(r=>r.Status == expectedStatus)));
You can verify that the AddRequestmethod gets called with parameter (Request) which has the correct Status. Also, mocking the Requestobject is not really necessary here.
您可以验证是否AddRequest使用Request具有正确Status. 此外,在Request这里模拟对象并不是真正必要的。

