This project is read-only.

Mocking members

Jun 23, 2011 at 6:11 PM

A colleague showed me how MOQ can create a mock of not only an object, but also the members of that object.

For example, if you have an interface ICar which has a property Controller which is defined as IControllable, then when you create a mock of ICar, MOQ creates a mock automatically for the Controller property too.

Is there any way to achieve a similar behaviour using NMock3? I have tried creating mocks with Stub, RecursiveStub, options...

Jul 27, 2011 at 6:08 PM

Hi Jim4u,

That is what the Stub is supposed to do.  I haven't spent much time with it, I have only converted it from what NMock2 did with it.  If it is something you are finding lots of uses for, I can look into it.

Jul 28, 2011 at 9:28 AM

Hello,

This will save us some work.  I create mocks of some objects by using the MockStyle.Stub parameter when I don't really care about the operations done on that object. If that object has properties that are again interface types, for example public IMovable Movable, I have to explicitly create mocks for them too. It would be nice if it can be like MOQ and I won't have to create stubs for the members when I have created a stub of the object.

Can you please let me know what RecursiveStub means?

Jim

Jul 28, 2011 at 5:09 PM

Hi Jim4u,

I am working on some unit tests for the Stub feature in NMock3.  Can you give me some examples of how you think it should work and I will get the code shored up.  I need to know if you expect to be able to stub event handlers and setting of properties.  I can only think of reasons to stub property gets and method calls.  I am looking into recursivestub and that looks like it may go away.  Stub may also go away because a Mock<T> created by using the MockStyle.Default because it supports stubbing automatically.  Here is an example

 

private Mock<IParentInterface> parentInterfaceMock;
parentInterfaceMock = Factory.CreateMock<IParentInterface>("parentInterfaceMock");


mock.Stub.Out.GetProperty(_ => _.ReadWriteObjectProperty, new Version(1, 2, 3, 4));
Version v = mock.MockObject.ReadWriteObjectProperty;
Version v2 = mock.MockObject.ReadWriteObjectProperty;

Assert.IsTrue(v.Major == 1 && v.Minor == 2 && v.Build == 3 && v.Revision == 4);
Assert.IsTrue(v2.Major == 1 && v2.Minor == 2 && v2.Build == 3 && v2.Revision == 4);

Aug 11, 2011 at 10:32 AM

Hello,

I'm sorry for the rather late reply.

As I said, it would be nice if Stub of a type will automatically create stubs of member properties also. No, setting of properties don't need to be taken care of. We just want to avoid creating stubs of member properties and therefore operations on the members of the properties. I don't know if we need to do anything about method calls.

I don't understand when you say that <Stub may also go away because a Mock<T> created by using the MockStyle.Default because it supports stubbing automatically.>. What if we don't care about any interactions with a mock object? Then, we can create a mock of that object by specifying MockStyle.Stub. If we use MockStyle.Default, we have to write code to Stub out interaction with each member like you have done.

Jim