tags:

views:

180

answers:

2

I have a class that acts like this at the moment:

public class MyClass {

    public void Method1(){
        if (false) {
            Method2();
        }
    }

    public void Method2(){
        //do something here
    }
}

So Method2 is never called (my code looks a bit different but I have this if-clause that evaluates to false and therefore doesn't execute the Method2. Checked it by debugging to be sure). I want to tell RhinoMocks that I expect Method2 to be called and the test to fail:

MockRepository mock = new MockRepository();
MyClass presenter = mock.PartialMock<MyClass>();

Expect.Call(() => presenter.Method2()).IgnoreArguments();

mock.ReplayAll();
presenter.Method1();
mock.VerifyAll();

...but the test passes.

(The reason for the lambda expression in the Expect.Call is, that my actual Method2 has arguments)

My questions:

  • Is this the usual approach for testing in this scenario? (I'm just starting with RhinoMocks and mocking-frameworks in general)
  • Why does the test pass?
+3  A: 

As confirmed by Jakob's comments, PartialMock only mocks abstract/virtual methods, so your expectation isn't actually doing anything.

Personally I don't generally mock out methods within the class I'm testing. I just mock out dependencies, and express those through interfaces instead of concrete classes, avoiding this problem to start with.

Jon Skeet
That is correct: PartialMock will only mock virtual and abstract methods.
Jakob Christensen
Great, thank you both.
Matthias Schippling
A: 

I think there is a lot of value in partially mocking out concrete classes. Say for example you have a couple private methods that get called within a class. You obviously would need to make them 'internal' before testing, but you would like to have a way to test this -- without making a new class (which isn't the right solution in a lot of cases).

To make this test fail you would just have the signature of Method 2 to:

public virtual void Method2(){}

Bryan Rowe