Rhino.Mocks, TypeMock

TypeMock too powerful to use

In my early stages of following TDD light I was looking for a mock object framework. I tried Rhino.Mocks and found it hard to use. Then I tried TypeMock and falled in love with the product. It allows mocking almost anything. With TypeMock I only failed to mock SqlDataReader because it doesn’t have a public constructor.

It was then. Today I believe that TypeMock is bad for the same reason I loved it before. It’s too powerful. It doesn’t force you to write a testable code.

The reason I found Rhino.Mocks hard to use is because I haven’t learn yet about Inversion of Control Containers and the Dependency Injection pattern. Today I write my own mock objects without using any framework. However, if you want to use a Mock Framework, I strongly recommend Rhino.Mocks.

If you’re not familiar with this pattern I recommend to read Jeremy Palermo’s article Simple dependency injection to get you started with unit testing.


7 thoughts on “TypeMock too powerful to use

  1. Interesting.

    I haven’t use Rhino.Mocks for a while but from what I can remember it could mock delegates, interfaces and virtual methods. I thus found I was either making all the methods virtual or was introducing interfaces, even though those interfaces were pretty much useless for anything other than testing.

    In fact I found designing for testability can actually cause me to write slightly poor code, but I guess it depends on the situation.

  2. Colin,

    I believe that making methods virtual and introducing interfaces is a good thing. By introducing interfaces you make you code loose coupled so if in the future you decide to expand your code it’s going to be much easier.

  3. This how you can mock SqlDataReader with TypeMock

    Mock mockSqlCon = MockManager.MockAll(typeof(SqlConnection));

    MockObject mockReader = MockManager.MockObject(typeof(SqlDataReader), Constructor.Mocked, null as SqlCommand);
    MockManager.MockAll(typeof(SqlCommand)).ExpectAndReturn(“ExecuteReader”, (SqlDataReader)mockReader.Object, 1);

  4. Is there any way we can mock Reader object using Rhino mock like using Type mock you have shown above. if you have any code please share I am struggling for that


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s