Check if a property was set - using Moq

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();

mock.Verify(m=>m.AddRequest(It.Is<Request>(r=>r.Status == expectedStatus)));

You can verify that the AddRequest method gets called with parameter (Request) which has the correct Status. Also, mocking the Request object is not really necessary here.


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();
}