Rhino.Mocks: method calls the recorder (aka spy)

I have a piece of logic that I want to test, and it uses an injection interface depending on one (or more) void methods, for example:

interface IMyService
{
    void MethodA (MyComplexObject arg1, int arg2);
}

I would like to create a stub for this IMyServicethat simply recorded the method calls MethodA, and later I could access it as a list, something like

MyComplexObject actualParameter = serviceRecorder
    .GetMethodRecordings("MethodA").GetRecord(10).GetInputParameter(0);

I need this to examine the contents of such a parameter for a specific call and make statements on it. I know that others have done this (for example, setting expectations of expectations with restrictions), but it is much easier to write for cases where you have many calls and you want, for example, to make statements only on the 51st.

So, is there any mechanism in Rhino.Mocks for this, or have I gone to my own devices (recording a dummy implementation IMyServicewith write capability)?

NOTE: (I know that this can lead to fragility of the tests, and I know the consequences).

UPDATE: this is what I have found so far (partly thanks to Mark's help in naming this template as Test Spy):

+3
source share
2 answers
// arrange
var myServiceStub = MockRepository.GenerateStub<IMyService>();
var myComplexObj = new MyComplexObject 
{
    SomeProp = "something",
    SomeOtherProp = "something else"
};

// act
myServiceStub.MethodA(myComplexObj, 10);

// assert
myServiceStub.AssertWasCalled(
    x => x.MethodA(
        Arg<MyComplexObject>.Matches(
            arg1 => arg1.SomeProp == "something" &&
                    arg1.SomeOtherProp == "something else"
        ), 
        Arg<int>.Is.Equal(10)
    )
);

Note. Remember to make the interface public or Rhino Mocks cannot create a proxy.


UPDATE:

, . , :

var args = myServiceStub.GetArgumentsForCallsMadeOn(
    x => x.MethodA(null, default(int)),
    x => x.IgnoreArguments()
);

var theComplexObjectPassedAtThe51thCall = (MyComlexObject)args[50][0];
// TODO: assert something on this object
+5

Arrange-Act-Assert (AAA) Rhino Mocks.

Record-Replay . , , - - .

Rhino Mocks 4, , Record-Replay, -, Moq.

, Test Double, , Test Spy - . xUnit Test Patterns :)

+3

Source: https://habr.com/ru/post/1722245/


All Articles