How can I get around the method execution in the RhinoMocks layout?

I use RhinoMocks for a very simple test (I have to say I'm a beginner here). I tried to mock my object like this

var mock = MockRepository.GenerateMock<MyClass>();

create a stub helper:

var stubLinkedObject = MockRepository.GenerateStub<MyClass>();

then follow some logic that should call the AddLinkclass method MyClasswith my stub argument. At the end of the test, I simply state that this method was actually called using

mockAction.AssertWasCalled(a => a.AddLink(stubLinkedObject));

I entered the correct dependency and the method is actually called. However, the problem is that the real's implementation is MyClasscalled and crashes because some logic simply cannot be executed (the collection of links is not available, etc.). How can I get around the execution and just check if the method is called? I tried something like

mockAction.Stub(a => a.AddLink(null)).IgnoreArguments().Do(null);

before I go into execution, but that doesn't seem to work (I only get exceptions). Any ideas and probably an explanation why the layout follows the logic of the method in general?

+3
source share
2 answers

I tried to reproduce. Here is the code that works great for me

[Test]
public void Test()
{
    var classMock = MockRepository.GenerateMock<MyClass>();
    var linkedMock = MockRepository.GenerateStub<MyClass>();

    classMock.Expect(c => c.MyMethod(linkedMock));

    classMock.MyMethod(linkedMock);

    classMock.AssertWasCalled(c => c.MyMethod(linkedMock));
}

public class MyClass
{
    public virtual void MyMethod(MyClass linkedClass)
    {
        Console.WriteLine("MyMethod is called");
    }
}
+4
source

, AddLink , .Net ( ).

( IMyClass MyClass). , - , , MyClass ( )

+3

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


All Articles