Moq - How to verify that a property value is set via the setter

Solution 1:

The following should work. Configure your mock object as:

var mock=new Mock<IContent>();
mock.SetupSet(content => content.IsCheckedOut=It.IsAny<bool>()).Verifiable();

And after the test code:

mock.VerifySet(content => content.IsCheckedOut=It.IsAny<bool>());

I haven't tested it anyway, so please tell me if it works for you.

EDIT. Indeed, this will not work since the setter for IsCheckedOut is false.

Anyway, now I see that you never set the value of IsCheckedOut at class construction time. It would be a good idea to add the following to the Content class:

public Content()
{
    IsCheckedOut=false;
}

Solution 2:

Mock mockContect = new Mock<Cotent>(); 
mockContent.VerifySet(x => x.IsCheckedOut, Times.Once());

Will that do the trick? Not sure how the private setter comes in to play as havent tested that. but works for my public setter.

Got this from: http://www.codethinked.com/post/2009/03/10/Beginning-Mocking-With-Moq-3-Part-2.aspx

Solution 3:

why don't you simply set up the content to be checked out to start with? Remember, you are only testing the behaviour of the CheckIn function.

[TestMethod]
public void CheckInSetsCheckedOutStatusToFalse()
{
    // arrange - create a checked out item
    Content c = new Content();
    c.CheckOut();

    // act - check it in
    c.CheckIn();

    // assert - IsCheckedOut should be set back to false
    Assert.AreEqual(false, c.IsCheckedOut);
}