I decided to try a mocking framework for the first time (I have always hand-coded my mocks before), mostly after Scott Bellwell rant about Mocking being bad.
SideNode: Personally, I thing Scott likes make confrontational statements to stir things up and make people think. Beside it gets his name out there.
So, I went to the TypeMock website and downloaded. First mistake, I was not paying attention that were two versions available, one 32 bit systems and one for 64 bit systems (I am running Vista 64), and I downloaded and install the 32 bit system. I wrote my first test:
[ClearMocks]
class ReserverationServicesTests
{
public class createAvailableReserverationForDate
{
[Fact]
[VerifyMocks]
public void createOneAvailalbleReservationForStoreOneTodaysDate()
{
IReservationRepository mockRepository = RecorderManager.CreateMockedObject<IReservationRepository>();
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
mockRepository.Save(1, DateTime.Today, DateTime.Parse("09:00 am"),reservationStatus.Free);
recorder.Return(true);
}
IReservationServices Service = new ReservationServices(mockRepository);
int slotsCreated = Service.createAvailableReserverationForDate(1, DateTime.Today, DateTime.Parse("09:00 am"), DateTime.Parse("09:30am"));
Assert.Equal(1, slotsCreated);
}
}
}
Fairly simple mocking of an interface. I run the test and receive an error that TypeMock needs to be linked with a coverage tool to run. Seemed to be a little strange but okay. I linked to one of the coverage tools listed. Run again, same error.... Off to the TypeMock user forums. Find the problem is that I am running 64 bits. I uninstall the 32 bit version of TypeMock Isolator (mistake #2), and install the 64 bits version. Startup Visual Studio and try to run the test. Receive an error that TypeMock is not enabled. I went to the Tools menu to enable TypeMock and there are no TypeMock menus. Back to the user forums, discover the both the 32 bit and the 64 bit version need to be installed (I think the 64 bit installer should check if VS is installed and install the 32 bit if necessary).
Now, with everything is setup correctly, I run the test and get an error:
TypeMock Verification: Unexpected Call to XXXX.Setup.IReservationRepository.Save()
TypeMock.VerifyException:
TypeMock Verification: Unexpected Call to XXXX.Setup.IReservationRepository.Save()
at TypeMock.Mock.a(String A_0, Object[] A_1, Object A_2, Object A_3, Int32 A_4, Boolean& A_5, String A_6)
at bf.a(String A_0, Object[] A_1, Object A_2, Object A_3, String A_4, Boolean& A_5)
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at MockIReservationRepository.Save(Int32 , DateTime , DateTime , reservationStatus )
I run the test and get and unimplemented exception, that was good, now to implement the createAvailableReserverationForDate method...Done.
Now, after all the trials of getting TypeMock installed, I admit that I was not very trusting of TypeMock. My first thought was "what do mean it was an unexpected call, I setup to expect the call".
Back to the user forums, I need to create a user account in order to post a message. I fill out the form to create the user account and get an error message on the web page:
Couldn't get mail server response codes
DEBUG MODE
Line : 115
File : smtp.php
Ouch, now my confidence in trying TypeMock is really down the tubes. Try again to create the same user account, can not since the username and email are already in use. Hmm, wonder if I can login with that username....nope it is an inactive account. Okay create another account, get the debug mode error again. Send an email to TypeMock about the create user error.
Staring at the test, about to rip out the TypeMock, when smacking my head, I realized I have not done something very simple, which was to step through the code. I step through the code, and discover that I had set the termination condition of a loop incorrectly and was calling the Save method more than once. Fix the loop condition, run the test and got the green light :). It is still too early to decide if TypeMock is for me but I do have couple suggestions:
- Improve the installation experience, have one install that install everything necessary for the machine.
- Improve the unexpected call message for example: "TypeMock Verification: Unexpected Call to XXXX.Setup.IReservationRepository.Save() [Expected: 1 but got more]