Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Inversion of control is your solution, not Mock objects. Here's why:</p> <p>You mock the interface to make sure that some code that utilizes your IEmployeeManager is using it properly. You aren't using the test code to prove IEmployeeManager works. So there has to be another class that takes an IEmployeeManager, for instance, which you will actually be testing with your mock object.</p> <p>If you are actually just testing EmployeeManager, you can do much better. Consider dependency injection. In this manner, you will expose a constructor for EmployeeManager that will take at least one parameter which is an interface. Your EmployeeManager code will internally use this interface for any implementation specific calls that it needs to make.</p> <p>See <a href="http://en.wikipedia.org/wiki/Strategy_pattern" rel="nofollow noreferrer">Strategy Pattern</a></p> <p>This will lead you into a whole, exciting world of <a href="https://stackoverflow.com/questions/3058/what-is-inversion-of-control">Inversion of Control</a>. And as you dig into that, you will find that problems like these have been effectively solved with IoC containers such as <a href="http://code.google.com/p/autofac/" rel="nofollow noreferrer">AutoFac</a>, <a href="http://ninject.org/" rel="nofollow noreferrer">Ninject</a>, and <a href="http://structuremap.net/structuremap/index.html" rel="nofollow noreferrer">Structure Map</a>, to name a few.</p> <p>Mocking interfaces is great, and you can mock an interface that you then pass into IoC. But you'll find that IoC is a much more robust solution to your problem. And yes, while you might only be implementing a second alternative just for testing, it is still important to do for that very reason -- seperating the strategy under test from the business logic of EmployeeManager.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload