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.



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.
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.
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);
vkreynin said
max, thanks for sharing.
CSLAEx, a nice dal, can be nicer? « maonet technotes said
[...] for CSLAEx we have to use TypeMock to “Mock” SafeDataReader. Possibly related posts: (automatically generated)IOC Container in [...]
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
CThru and Silverunit - Adiel Sharabi said
[...] אחרים. הייחוד שלה הוא שהיא מאד מאד חזקה בעניין (יש שיאמרו חזקה מידיי), היא יכולה להיכנס עמוק עמוק לקרביים של האובייקט הנבחן [...]