1

I am new to mocking and stubbing. I have to write unit tests to a class, which I cannot redesign. It is what it is. The class has public methods that have to be tested. These public methods use private methods to perform external calls to web services. The class under the test is not designed for easy dependency injection. The private methods use System.Net.WebRequest to perform web service calls.
I need to mock these private methods. What is the best mocking framework for the task? What is the technique to mock these private methods?

Ramzay
  • 181
  • 4
  • 12
  • I'm assuming the class under test is not an interface implementation? –  Oct 29 '14 at 14:52
  • It is not. The task is to introduce unit tests with as little impact on the functional code as possible. – Ramzay Oct 29 '14 at 15:00
  • If you could extract an interface then you could easily mock at least the public methods. Mocking doesn't mean testing of course. If that's not a viable option I know there are some isolation frameworks that work at IL level. I know they exist but I have never used them. –  Oct 29 '14 at 15:03
  • [Telerik JustMock](http://www.telerik.com/help/justmock/advanced-usage-mocking-non-public-members-and-types.html) allows you to mock non-public methods. However, you already alluded to the obvious: you'd be better off in the long run refactoring/redesigning to allow dependency injection, and preventing the "need" to mock private methods. – hunch_hunch Oct 29 '14 at 15:05
  • I should mention that only the paid version of JustMock allows you to mock private methods. – hunch_hunch Oct 29 '14 at 15:16
  • The question goes to the core of the purpose of mocking: remove the external dependency from the test. In this case and as I believe in many similar cases the dependency is not external to the unit under test, but internal and buried inside privet methods. It is a very unfortunate design for unit testing. But it is what it is. I need to mock not the class, but the methods in the class under test. – Ramzay Oct 29 '14 at 15:29

1 Answers1

0

It seemed like I had found the solution. The answer is Microsoft Moles, because the project is in VS2010. I guess if it were VS2012 or VS2013 I would have to use Microsoft Fakes.

Anyway, it worked just fine. Moles allow not only mock the class under test, but also select private methods of the class.

Ramzay
  • 181
  • 4
  • 12