RhinoMock : Mocks Vs StrictMocks Vs DynamicMocks

2019-02-04 08:50发布

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.

2条回答
虎瘦雄心在
2楼-- · 2019-02-04 09:50

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();
}
查看更多
Summer. ? 凉城
3楼-- · 2019-02-04 09:52

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.

查看更多
登录 后发表回答