问题
I understand the difference between a Mock and a Stub.
But different types of Mocks in RhinoMock framework confuses me.
Could someone explain the concepts of Mocks Vs StrictMocks Vs DynamicMocks in terms of RhinoMock framework.
your answers are greatly appreciated.
回答1:
A strict mock is a mock that will throw an exception if you try to use any method that has not explicitly been set up to be used.
A dynamic (or loose) mock will not throw an exception if you try to use a method that is not set up, it will simply return null a default value from the method and keep going.
It is highly recommended to use dynamic mocks, as strict mocks generally turn out to be a maintenance nightmare. Here's a good blog post that has a code example of strict vs. dynamic, and why strict mocks are usually a bad idea.
回答2:
Strongly disagree on this point.
Arguably Test Driven Development is not possible using dynamic mocks, because what you are testing is not necessarily what you are implementing.
Imagine you added a foreach loop where you made a db call inside the loop. This scales very badly. If you used dynamic mocks to mock your dependencies, you would potentially miss mocking the db calls, hence missing the scalability issue because you wouldn't need to strictly mock every db call.
public void myMethod()
{
externalMethod1.doSomething();
foreach()
{
externalDbCall.doSql();
}
}
public void testMyMethodWithDynamicMocksPassesAndMissesDbCallInLoop()
{
expect(externalMethod1.doSomething();
}
public void testMyMethodWithStrictMocksFailsAndHighlightsDbCallInLoop()
{
expect(externalMethod1.doSomething();
}
来源:https://stackoverflow.com/questions/3526195/rhinomock-mocks-vs-strictmocks-vs-dynamicmocks