Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I bypass the execution of a method in a RhinoMocks mock?

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 helper stub :

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

then execute some logic which should call the method AddLink of the class MyClass with my stub argument. At the end of the test I simply assert that this method was actually called with

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

I injected the correct dependency and the method is actually called. However, the problem is that the real implementation in MyClass is called and results in crash because some logic just can't be executed (link collection is not available etc.). How can I bypass the execution and simply check whether a method is called ? I have tried something like

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

before I go into the execution but this doesn't seem to work(I only get some exceptions). Any ideas and probably an explanation why the mock is executing the method logic at all ?

like image 516
Tomas Vana Avatar asked Dec 02 '25 21:12

Tomas Vana


1 Answers

I've tried to reproduce. Here is the code which works fine 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");
    }
}
like image 133
dh. Avatar answered Dec 04 '25 09:12

dh.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!