Mock object that implements get and sets not defined in the interface

Using Moq, RhinoMocks or a similar structure, is there a way to configure the layout for the implementation and get and set for all properties of the object, even if the interface does not work?

I can, of course, mock objects manually, but would prefer to use an isolation environment such as Moq or RhinoMocks to avoid creating a bunch of template classes.

Here is a 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 as is code will throw an exception in mockObject.Property1 because the IMyObject.Property1 property does not have access to the accessory.

Thanks DanO

+3
source share
4 answers

Rhino Mocks, . , , . , :

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

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

mock.VerifyAllExpectations();

, , Property1 ​​ "Test" - mock.Expect mock.MethodUnderTest (, Property1 ClassUnderTest).

, , , IgnoreArguments Expect, :

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

- GetArgumentsForCallsMadeOn. , "" . :

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

, Moq , .

+1
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; }
}
+4

Moq, :

var mock = new Mock<IMyObject>();

// test code goes here

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

SetupAllProperties .

+1

:

var mock = new Mock<IMyObject>();

// your test code

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

All Articles