Vadim's Weblog

Never stop learning.

TypeMock too powerful to use

Posted by Vadim on April 10, 2007

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.

Advertisements

7 Responses to “TypeMock too powerful to use”

  1. Colin Jack said

    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. vkreynin said

    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. max said

    This how you can mock SqlDataReader with TypeMock

    MockManager.Init();
    Mock mockSqlCon = MockManager.MockAll(typeof(SqlConnection));
    mockSqlCon.ExpectCall(“Open”);
    mockSqlCon.ExpectCall(“Close”);

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

  4. vkreynin said

    max, thanks for sharing.

  5. […] for CSLAEx we have to use TypeMock to “Mock” SafeDataReader. Possibly related posts: (automatically generated)IOC Container in […]

  6. Anonymous said

    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

    thanks
    Ravi

  7. […] אחרים. הייחוד שלה הוא שהיא מאד מאד חזקה בעניין (יש שיאמרו חזקה מידיי), היא יכולה להיכנס עמוק עמוק לקרביים של האובייקט הנבחן […]

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

 
%d bloggers like this: