4

I have a class similar to the following:

public class MyProxy : ClientBase<IService>, IService { public MyProxy(String endpointConfiguration) : base(endpointConfiguration) { } public int DoSomething(int x) { int result = DoSomethingToX(x); //This passes unit testing int result2 = ((IService)this).DoWork(x) //do I have to extract this part into a separate method just //to test it even though it's only a couple of lines? //Do something on result2 int result3 = result2 ... return result3; } int IService.DoWork(int x) { return base.Channel.DoWork(x); } } 

The problem lies in the fact that when testing I don't know how to mock the result2 item without extracting the part that gets result3 using result2 into a separate method. And, because it is unit testing I don't want to go that deep as to test what result2 comes back as... I'd rather mock the data somehow... like, be able to call the function and replace just that one call.

5 Answers 5

3

Do you have a preference for mocking frameworks?

The Partial Mock feature in Rhino Mocks seems like it should do what you want.

Sign up to request clarification or add additional context in comments.

4 Comments

I was hoping to utilize the built-in Microsoft Testing Framework, but it seems like that might not be a solution. I'm really hoping to avoid anything beyond what Visual Studio Ultimate involves.
I'm curious, do you have a reason for avoiding things beyond what's built into studio? Also, which version of Studio are you using?
Everything outside of what is considered an official Microsoft solution takes forever to approve. It took a long time to approve jQuery. Everything has to go through a rigurous security test, usability test, debugging test, etc. And since I need to keep going on my project I was hoping to find a Microsoft (or code-based) solution.
Ah, non-technical requirements :) Then yes, since you can modify your code, you're better off refactoring it to make testing easier (eg making the implementation of DoWork pluggable).
2

You can't really do that. You have three choices:

  • Subclass MyProxy and override DoWork, which will require some fiddling to please the compiler
  • Mock the Channel property, which will require that it is settable in the base class
  • Move DoWork out into another class, pass it the Channel in the constructor, and mock that in your tests

Comments

1

Do the following:

Set up an IService property such as:

public IService MyService { get; set; }

Then you can do: int result2 = MyService.DoWork(x) as long as somewhere in the constructor or whatever you set MyService = this;

If you don't want to expose the property you can make it private or whatever and test it using accessors.

Comments

0

You can do it by using latest Microsoft Research project Moles

Once you get it running, you can do following

MMyProxy.DoWork32 = () => put your mock result here. 

Remember to set moleBehavior to fall-through for the unmocked methods.

Comments

0

I believe that you have a design issue here, your IService.DoWork should most likely live in another class, it looks like just a thin wrapper on something else. Have you considered refactoring it?

Then if it lives in another class you don't need any special handling for mocking.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.