Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rhino Mocks stubs and mocks are only good for interfaces?

Tags:

Is it correct that Rhino Mocks stubs and mocks are only good for interfaces, not concrete classes? I spent quite a time trying to make this piece of code working. I did not expect the stubbed pubSubClient to always call Send method from the class. That method has some dependencies and throws exception.

[Test] public void Test01() {     PubSubMessage psm = new PubSubMessage();      var pubSubClient = MockRepository.GenerateStub<PubSubClient>();     pubSubClient.Stub(x => x.Send(psm)).IgnoreArguments().Return(null);     // actual PubSubClient Send method throws exception     // the rest of the test is skipped... } 

However, when I have extracted the interface and run the same test with IPubSubClient, it seems to be working as expected.

Does it mean that I have to extract the interface for every class I want to mock/stub with Rhino? Or I am missing something, technically or conceptually?

UPDATE: OK, It seems I figured out what part I was missing: Rhino Mocks cannot intercept calls to non-virtual methods. So, I guess I have either use interfaces or make every method on the concrete class virtual. Please correct me if there's another option.

like image 608
Dmitry Duginov Avatar asked Mar 05 '10 15:03

Dmitry Duginov


People also ask

What is the difference between mocks and stubs?

Stub: a dummy piece of code that lets the test run, but you don't care what happens to it. Substitutes for real working code. Mock: a dummy piece of code that you verify is called correctly as part of the test. Substitutes for real working code.

Which is a valid mock type using Rhino?

Mock OptionsStrict Mock. A strict mock requires you to provide alternate implementations for each method/property that is used on the mock. If any methods/properties are used which you have not provided implementations for, an exception will be thrown. Dynamic Mock.

What are stubs and mocks in unit testing?

Mocking and stubbing of internal functions A great application of mocks and stubs in a unit/component test is when your implementation interacts with another method or class. You can mock the class object or stub the method behavior that your implementation is interacting with.


1 Answers

Bryan's answer of using partial mocks is incorrect. That's not what partial mocks are for.

Jon Erickson's answer is mostly correct: Rhino Mocks and Moq can't intercept non-virtual calls, nor can they intercept static methods or properties. That means you can't fake the following:

DateTime.Now; // static property, can't fake static property someClass.SomeNonVirtualMethod(); // can't fake non-virtual method sealedClass.Foo(); // can't fake anything on sealed classes Utilities.SomeStaticMethod(); // can't fake static methods someList.Any(); // can't fake extension methods like Linq's .Any() 

TypeMock can fake these, as Jon mentioned.

It should be noted there is an additional mocking framework that can intercept all calls: the Microsoft Moles framework. It works the same way TypeMock does, it uses the .NET profiler API to intercept calls.

Moles is free (for now). It's also beta. Moles only only works with Microsoft Pex tools. And its API is clearly inferior to TypeMock's refined, elegant API.

like image 126
Judah Gabriel Himango Avatar answered Nov 07 '22 07:11

Judah Gabriel Himango