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)
On 9/1/09, Mark Whitfeld <[email protected]> wrote: > > 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; > } > > > > > On Sep 1, 12:53 pm, Mark Whitfeld <[email protected]> wrote: >> 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 > > > -- Sent from my mobile device Tim Barcz Microsoft ASPInsider http://timbarcz.devlicio.us http://www.twitter.com/timbarcz --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rhinomocks?hl=en -~----------~----~----~----~------~----~------~--~---
