Since it only take a couple of lines of code to create your mock and set the expectation, why not just do it right inside the method? The mock is specific to that test anyway so by sharing the creation of the mock, there's a greater chance that someone will tweak the shared code in the future and possibly break the unit tests.
--- Patrick Steele http://weblogs.asp.net/psteele On Mon, Sep 19, 2011 at 7:25 AM, VG <[email protected]> wrote: > Hi, > > I am having problem while unit testing an Interface Method for > different values . I want to return different values for a method > while mocking it . Currently I am creating multiple mock objects to > return multiple values. I will paste my controller code,mock methods > and unit tests that I have written to perform the test . > > This is my controller code : > [Transaction] > public ActionResult CopyEvent() > { > int copiedEvent; > try{ > copiedEvent = _symposiaEventQueries.MakeCopyOfEvent(); > > TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] > = > "The Symposia Event was successfully copied. > Please make any necessary adjustments"; > }catch(Exception ex){ > return Content("Error: "+ex.Message); > } > return Content(copiedEvent.ToString()); > } > > I am creating mock objects for symposiaEventQueries as below : > > public static ISymposiaEventQueries > CreateMockSymposiaEventQueriesWhenMakeCopyOfEventReturnstwo() > { > var mockedRepository = > MockRepository.GenerateMock<ISymposiaEventQueries>(); > mockedRepository.Expect(mr => > mr.MakeCopyOfEvent()).Return(2); > return mockedRepository; > } > > public static ISymposiaEventQueries > CreateMockSymposiaEventQueriesWhenMakeCopyOfEventReturnsOne() > { > var mockedRepository = > MockRepository.GenerateMock<ISymposiaEventQueries>(); > mockedRepository.Expect(mr => > mr.MakeCopyOfEvent()).Return(1); > return mockedRepository; > } > > public static ISymposiaEventQueries > CreateMockSymposiaEventQueriesCopyEventThrowsExpection() > { > var mockedRepository = > MockRepository.GenerateMock<ISymposiaEventQueries>(); > mockedRepository.Expect(mr => > mr.MakeCopyOfEvent()).Throw(new Exception("")); > return mockedRepository; > > I have created three diff methods to return the ISymposiaEventQueries > object. While unit testing I call respective methods to perform the > unit testing. > > The unit Test code is below : > > [Test] > public void > CanCopyEventWhenEventIdExistsReturnsNewEventIdAsContent() { > _symposiaEventQueries = > SharedMockInterfaces.CreateMockSymposiaEventQueries(); > var result = _controller.CopyEvent(); > > var content = (ContentResult) (result); > Assert.AreEqual(content.Content, "2"); > > _symposiaEventQueries.AssertWasCalled(x=>x.MakeCopyOfEvent(0,0),y=>y.IgnoreArguments()); > > _controller.TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()].ToString() > .AssertSameStringAs("The Symposia Event was successfully > copied. Please make any necessary adjustments"); > } > > [Test] > public void CanNotCopyEventWhenCopyEventThrowsException() { > _symposiaEventQueries = > SharedMockInterfaces.CreateMockSymposiaEventQueriesCopyEventThrowsExpection(); > var content = (ContentResult)_controller.CopyEvent(); > content.Content.AssertSameStringAs("Error: "); > } > > Can anybody give me better approach to this , I think i doing it the > wrong way. > > Thanks in advance. > Vishal > > -- > 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. > > -- 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.
