Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a property was set - using Moq

Tags:

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?

like image 820
mithun_daa Avatar asked Jun 04 '13 20:06

mithun_daa


People also ask

How do you set a mock object property?

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.

What does Moq verify do?

Verifies that all verifiable expectations have been met.

Is Moq a testing framework?

The Moq framework is an open source unit testing framework that works very well with .

Can you mock a private method Moq?

Moq supports mocking protected methods. Changing the methods to protected , instead of private , would allow you to mock their implementation.


2 Answers

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(); 
like image 95
neontapir Avatar answered Sep 20 '22 13:09

neontapir


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(); } 
like image 22
Ufuk Hacıoğulları Avatar answered Sep 19 '22 13:09

Ufuk Hacıoğulları