views:

111

answers:

4

I'm using MOQ with tdd, i'm comfortable with the simple mock objects. Now moving on to the complex objects(i.e., objects containing other objects as properties ). i have problem creating mocks for three scenarios.

  1. when the method being tested wraps a static method.

  2. when the method being tested wraps a read only property.

  3. when the method being tested calls another method belonging to the same class.

how do i create mocks for them.how do i resolve these scenarois, are could i go foreward changing the implementation of the tested method.?

any suggesstions will be helpful.

thanks.

+2  A: 

There are two ways around this. 1) introduce the necessary levels of abstraction, that will let you mock/stub as needed or 2) use TypeMock Isolator, which will allow you to mock almost anything through some clever (too clever, some say) IL re-engineering.

Brian Rasmussen
A: 

I don't know about MOQ but in RhinoMocks you can make something called a PartialMock which allows you to mock parts of a object but leave the other alone, anything with a expectation on it will be mocked.

Of course that only works if the methods in question are public.

A lot of times in cases like this Ill just use a hand-rolled mock by subclassing the class being testing and overriding the methods wrapping the static calls and other undesirables. You can also introduce a shunted constructor in the subclass to set up your read-only properties for testing.

ryber
A: 

Because the proxy object is inherited from the target . i dont think it is possible to mock the target object using moQ , may be we should try Type Mock Isolator under these complex scenarios

vijaysylvester
A: 

If you own the troublesome code, I would suggest refactoring it to make it more testable. If you have to resort to using an expensive tool to allow mocking of your own code, it's usually a sign that it can be refactored.

On the other hand, if you don't own that code or the code cannot change for good reason (sometimes testability and performance may be mutually exclusive concerns), TypeMock is definitely worth a look as it can create test doubles for static types.

It would help if you posted some code examples demonstrating what you're trying to do.

Mark Simpson