If you have similar expectations for 10-12 methods, I would put the
setup of those expectations in another method, but defer creation and
set up of the one particular method inside your unit test:
public void Test()
{
var mock = MockRepository.GenerateMock<ISomething>();
SetUpStandardExpectations(mock);
mock.Expect(c => c.DoSomething()).Throw(new ApplicationException());
...
}
Just my 2 cents... :)
---
Patrick Steele
http://weblogs.asp.net/psteele
On Mon, Sep 19, 2011 at 10:40 AM, vishal.gupta <[email protected]> wrote:
> Hi Patrick,
> This is just an small example, I have Interfaces which have 10-12 methods
> and each method is being tested for different values. I did not put that
> code here as it will be difficult to understand. I would like to know if
> there is better way to achive this thing as I have to create a mock object
> for each and every thing.
>
> Thanks.
> Vishal
>
> On Mon, Sep 19, 2011 at 4:55 PM, 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.