Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RhinoMocks - when a method is called n times, how to test its parameters in the n - k call

I have a method which calls another method exactly 4 times, each time with different parameters. I thought of writing 4 different unit test cases to check method is called with a specific value for each call.

Here is how my method looks:

public void MainMethod()
{
    IServiceProvider serviceProvider = GetServiceProvider();

    string value1 = GetValueFromStorage("SomeArg1");
    // Call AnotherMethod
    serviceProvider.AnotherMethod(value1);

    string value2 = GetValueFromStorage("SomeArg2");
    // Call AnotherMethod
    serviceProvider.AnotherMethod(value2);

    string value3 = GetValueFromStorage("SomeArg3");
    // Call AnotherMethod
    serviceProvider.AnotherMethod(value3);

    string value4 = GetValueFromStorage("SomeArg4");
    // Call AnotherMethod
    serviceProvider.AnotherMethod(value4);
}

And here is my test method:

public void TestMainMethod()
{
    // Stub storage
    IDataStorage dataStorage = MockRepository.GenerateStub<IDataStorage>();

    // Stub serviceProvider
    IServiceProvider dataStorage = 
         MockRepository.GenerateStub<IServiceProvider>();

    // stub for SomeArg1
    dataStorage.Stub(x => x.GetValueFromStorage(null)
                           .IgnoreArguments().Return("Value1"))
                           .Repeat.Once();

    // stub for SomeArg2
    dataStorage.Stub(x => x.GetValueFromStorage(null)
                           .IgnoreArguments().Return("Value2"))
                           .Repeat.Once();

    // stub for SomeArg3
    dataStorage.Stub(x => x.GetValueFromStorage(null).IgnoreArguments()
                           .Return("Value3")).Repeat.Once();

    // stub for SomeArg4
    dataStorage.Stub(x => x.GetValueFromStorage(null).IgnoreArguments()
                           .Return("Value4")).Repeat.Once();

   // call MainMethod
   MainMethod();

   // Assert that third call is called with "Value3"
   serviceProvider.AssertWasCalled(x => x.AnotherMethod("Value3"));
}

Here is seems that I can't ignore other calls and just verify that the third call is called with a specific argument(or for that matter any other call in the sequence). It seems that I have to call the "AssertWasCalled" four times and check individual argument in order. So how I can achieve this? Or am I missing something here?

like image 411
24x7Programmer Avatar asked Jan 03 '13 09:01

24x7Programmer


1 Answers

I think you can use GetArgumentsForCallsMadeOn(Action<T>). Long time since I've used it but it gives you a list with an array of objects which are the call parameters for each call made.

like image 147
Marwijn Avatar answered Oct 15 '22 12:10

Marwijn