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?
As confirmed by Jakob’s comments,
PartialMockonly 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.