How to use partial mocks

26 views
Skip to first unread message

Mark Whitfeld

unread,
Sep 1, 2009, 6:53:53 AM9/1/09
to Rhino.Mocks
Firstly, thanks for an amazing Mocking framework. I especially love
the type safety given using lambdas and the AAA syntax.

My only problem has been with trying to do a partial mock.
Many times I am testing for a specific method call on an interface,
and I pretty much just want the rest of the methods on the interface
to return something useful. So my first thought was to try to create a
partial mock for a class that implements the interface and then just
'override' the specific method of the interface using Rhino Mocks.
Not matter what I tried I couldn't get this to work. Any Suggestions?

I also need to test the scenario where I need to check that one method
on a class is using/calling another method on the class. I tried to
use Partial mocks for this, but couldn't get that to work either. Is
there something that I'm doing wrong?

Here is a simple code sample. Please could you show me how to get the
last test working. Thanks.
Here it goes:

public class MyClass
{
public virtual string MethodA()
{
return "Hello World";
}

public virtual string MethodB()
{
return string.Format("'{0}'", MethodA());
}
}

[TestFixture]
public class TestExample
{

[Test]
public void Test_MethodA_ShouldReturnHelloWorld()
{
//---------------Set up test pack-------------------
MyClass myClass = new MyClass();
//---------------Assert Precondition----------------
//---------------Execute Test ----------------------
string returnedString = myClass.MethodA();
//---------------Test Result -----------------------
const string expected = "Hello World";
Assert.AreEqual(expected, returnedString);
}

[Test]
public void Test_MethodB_ShouldReturnQuotedHelloWorld()
{
//---------------Set up test pack-------------------
MyClass myClass = new MyClass();
//---------------Assert Precondition----------------
//---------------Execute Test ----------------------
string returnedString = myClass.MethodB();
//---------------Test Result -----------------------
const string expected = "'Hello World'";
Assert.AreEqual(expected, returnedString);
}

[Test]
public void Test_MethodB_ShouldUseMethodA()
{
//---------------Set up test pack-------------------
const string otherReturnValue = "Hello Universe";
MyClass myClass = new MockRepository().PartialMock<MyClass>
();
myClass.Stub(t => t.MethodA()).Return
(otherReturnValue).Repeat.Any();
myClass.Stub(t => t.MethodB()).CallOriginalMethod
(OriginalCallOptions.NoExpectation);
//---------------Assert Precondition----------------
Assert.AreEqual(otherReturnValue, myClass.MethodA());
//---------------Execute Test ----------------------
string returnedString = myClass.MethodB();
//---------------Test Result -----------------------
string expected = string.Format("'{0}'",
otherReturnValue);
Assert.AreEqual(expected, returnedString);
}
}

Any Ideas?

Thanks
-Mark Whitfeld

Mark Whitfeld

unread,
Sep 1, 2009, 7:12:30 AM9/1/09
to Rhino.Mocks
Ah, ok I have solved my own problem within moments of posting :)

The last test should read:

[Test]
public void Test_MethodB_ShouldUseMethodA()
{
//---------------Set up test pack-------------------
const string otherReturnValue = "Hello Universe";
MockRepository mockRepository = new MockRepository();
MyClass myClass = mockRepository.PartialMock<MyClass>();
myClass.Stub(t => t.MethodA()).Return(otherReturnValue);
myClass.Replay();
//---------------Assert Precondition----------------
Assert.AreEqual(otherReturnValue, myClass.MethodA());
//---------------Execute Test ----------------------
string returnedString = myClass.MethodB();
//---------------Test Result -----------------------
string expected = string.Format("'{0}'", otherReturnValue);
Assert.AreEqual(expected, returnedString);
}

I was missing the "myclass.Replay();" call.
I think that the basis for my mistake is that I usually use the AAA
syntax and stubs, and for Partial mocks I could not use this in the
normal way.
Is there any reason why there is no 'Partial Stub'?
All I would probably have needed was a static method on MockRepository
called something like "GeneratePartial<T>":

public static T GeneratePartial<T>(params object[]
argumentsForConstructor)
where T : class
{
MockRepository repository = new MockRepository();
T obj = repository.PartialMock<T>(argumentsForConstructor);
repository.Replay(obj);
return obj;

Tim Barcz

unread,
Sep 1, 2009, 7:38:30 AM9/1/09
to rhino...@googlegroups.com
With the release of rhino 3.6 you should be able to do Partials using
AAA syntax (that is without the creation of a MockRepository)
--
Sent from my mobile device

Tim Barcz
Microsoft ASPInsider
http://timbarcz.devlicio.us
http://www.twitter.com/timbarcz

Mark Whitfeld

unread,
Sep 2, 2009, 6:22:33 AM9/2/09
to Rhino.Mocks
Ah, thanks.
Ahead of me yet again :)

Is there any reason why PartialMultiMock is not part of the
MockRepository static methods for AAA?
Also, should there not be a way of doing a PartialMultiMock where the
Interface methods not fulfilled by the partial mock class have stub
behaviour.
Currently I have to add the stub behaviour for each method on the
Interface manually.
So, I guess I am looking for a PartialMultiStub :) Is there any way of
achieving this?

On Sep 1, 1:38 pm, Tim Barcz <timba...@gmail.com> wrote:
> With the release of rhino 3.6 you should be able to do Partials using
> AAA syntax (that is without the creation of a MockRepository)
>
Reply all
Reply to author
Forward
0 new messages