DanO
DanO

Reputation: 983

mock object that implements gets and sets not defined in interface

Using Moq, RhinoMocks or a similar framework, is there a way to configure a mock to implement both get and set on all properties of an object even if the interface does not?

I can certainly mock the objects manually, but would prefer to use an isolation framework like Moq or RhinoMocks to avoid creating a bunch of boilerplate classes.

Here's some sample code:

//interface to be mocked
public interface IMyObject
{
    string Property1 { set; }
}

//test method code
var mock = Moq.Mock<IMyObject>();

//...some code here to configure all properties on mock to have a get and set...

var mockObject = mock.Object;

ClassUnderTest obj = new ClassUnderTest(mockObject);
obj.MethodUnderTest();

Assert.IsTrue(!String.IsNullOrEmpty(mockObject.Property1));    

Running the code as is will throw an exception on mockObject.Property1 because the IMyObject.Property1 property lacks the get accessor.

Thanks, DanO

Upvotes: 3

Views: 1061

Answers (4)

Kenneth Xu
Kenneth Xu

Reputation: 1514

For list:

var mock = new Mock<IMyObject>();

// your test code

mock.VerifySet(x => x.Property1 = It.Is<IList<int>>(l => l != null && l.Count > 0));

Upvotes: 1

kzu
kzu

Reputation: 1871

public class Test
{
    public void AssertOneElement()
    {
        var mock = new Mock<IMyObject>();
        var list = default(IList<int>);

        mock.SetupSet(x => x.Property1 = It.IsAny<IList<int>>())
            .Callback<IList<int>>(value => list = value);

        // Fails
        mock.Object.Property1 = new List<int>();

        // Succeeds
        // mock.Object.Property1 = new List<int>(new[] { 1 });

        Debug.Assert(list != null);
        Debug.Assert(list.Count == 1, "Called with a list that did not contain a single element!");
    }
}

public interface IMyObject
{
    IList<int> Property1 { set; }
}

Upvotes: 4

Steven Oxley
Steven Oxley

Reputation: 6723

Using Rhino Mocks, you could set an expectation for the property. This basically states that you expect the property to be set to a certain value. Using your example, an example test method body would be this:

var mock = MockRepository.GenerateMock(); mock.Expect(x => x.Property1 = "Test");

var classUnderTest = new ClassUnderTest(mock);
classUnderTest.MethodUnderTest();

mock.VerifyAllExpectations();

This would check to see if Property1 had been set to "Test" at some point between the call to mock.Expect and the call to mock.MethodUnderTest (technically, Property1 could be set in the constructor of ClassUnderTest).

In order to test that the property was set and ignore what it was actually set to, just chain IgnoreArguments to the return of the Expect call, like so:

mock.Expect(x => x.Property1 = "Test1").IgnoreArguments();

One way to test more complex write-only properties is to use the GetArgumentsForCallsMadeOn method. This allows you to retrieve a list of the arguments passed to each "call" of the property. The code to do this would be like the following:

var mock = MockRepository.GenerateMock();

var classUnderTest = new ClassUnderTest(mock);
classUnderTest.MethodUnderTest();

//the argument in the Action is ignored, so just use null
//Property1 is of type List<string>
var arguments = mock.GetArgumentsForCallsMadeOn(x => x.Property1 = null);

//arguments[0] contains the list of arguments for the first "call" of the
//property the first index (0) of that would contain the first argument
var firstCallArguments = arguments[0];
var firstArgument = (List<string>)firstCallArguments[0];
Assert.AreEqual(3, firstArgument.Count);

I am sure that Moq has similar functionality if you'd like to use that, instead.

Upvotes: 1

TrueWill
TrueWill

Reputation: 25523

Using Moq, this works:

var mock = new Mock<IMyObject>();

// test code goes here

// Note single equals sign, not double equals
mock.VerifySet(x => x.Property1 = "expected");

SetupAllProperties does NOT work.

Upvotes: 1

Related Questions