Using Moq ExpectSet
I am trying out Moq, having used RhinoMocks a fair bit in the past.
I was having trouble using the ExpectSet method which verifies that a property has been set, and a Google search found nothing that directly answered my question. I wanted to know how to verify that the value set is what was expected.
It turns out that you need to define a callback in which you have the assertion to verify that the property has been set to the correct value.
[TestMethod]
public void TestAppleShiner() {
// Mock the interface being passed to the class to be tested
var fruit = new Mock<IFruit>();
// Define the expectation that the Colour property will be
// set to Green
fruit.ExpectSet(f => f.Colour).Callback(
setColor => Assert.AreEqual("Green", setColor)).Verifiable();
// Run the test
ApplePolisher applePolisher = new ApplePolisher();
applePolisher.Polish(fruit.Object);
// Verify that the test passed (note .Verifiable on the ExpectSet)
fruit.VerifyAll();
}
public interface IFruit {
string Colour { get; set; }
}
public class ApplePolisher {
public void Polish(IFruit fruit) {
fruit.Colour = "Green";
}
}
After ExpectSet, call the Callback method, giving the name of the variable to hold the passed-in value, and then the assertion with regards to its value.