I am new to Moq and testing in general so here is my noobish Q. How do I test if the Status property on Request has been set using 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?
You can set the mock object property to the value returned by the mock object method. To achieve this, specify separate behaviors for the method and the property. You can specify one behavior at a time. For more information about mock object behavior, see Specify Mock Object Behavior.
Verifies that all verifiable expectations have been met.
The Moq framework is an open source unit testing framework that works very well with .
Moq supports mocking protected methods. Changing the methods to protected , instead of private , would allow you to mock their implementation.
I think VerifySet is the right approach. It would look something like this:
//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:
//Arrange var mock = new Mock<IDRepository>(); var mockRequest = new Mock<Request>(); mockRequest.SetupAllProperties();
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With