Thursday, April 15, 2010

Taking Telerik’s Just Mock for a test drive

Telerik has released a beta of JustMock (a mocking framework) they claim can easily mock anything including sealed classes, non-virtual methods, and static classes.  If they can do this great!, but lets see just how well this actually works by taking a look at some of the biggest pains to test: HttpContext and SqlConnection.

 

HttpContext Test

Test: Can I mock HttpContext, set a value, and get the value, in this case Error
   1: ErrorLoger target;
   2: private const string TestError = "Test Error";
   3: public MockILogger MockLogger;
   4:  
   5: [SetUp]
   6: public void Setup()
   7: {
   8:     target = new ErrorLoger();
   9:     HttpContext mockContext = Mock.Create<HttpContext>();
  10:     Mock.Arrange(() => mockContext.Error).Returns(new Exception(TestError));
  11:     target.Context = mockContext;
  12:     MockLogger = new MockILogger();
  13:     target.LoggerInterface = MockLogger;
  14: }
  15:        
  16: [Test]    
  17: public void LogError_SetsLoggerMessageEqualToContextErrorMessage_Test()
  18: {
  19:    target.LogError();
  20:  
  21:    Assert.AreEqual(TestError,MockLogger.Message);
  22: }


Result: Just Mock crashes when it tries to mock HttpContext.

SqlConnection Test

Test: Can I mock a SqlConnection and open a connection with out really opening a connection.
   1: private DataRequest target;
   2: private bool SqlConnectionOpened;
   3:  
   4: public void SqlConnectionOpenedCalled()
   5: {
   6:     SqlConnectionOpened = true;
   7: }
   8:  
   9: [SetUp]
  10: public void Setup()
  11: {
  12:     SqlConnectionOpened = false;
  13:     target = new DataRequest();
  14:     
  15:     var mockSqlConnection = Mock.Create<SqlConnection>();
  16:     Mock.Arrange(() => mockSqlConnection.Open()).DoInstead(SqlConnectionOpenedCalled);
  17:     target.Connection = mockSqlConnection;
  18: }
  19:  
  20: [Test]
  21: public void GetUserList_Test()
  22: {           
  23:     List<string> actual = target.GetUserList();
  24:     Assert.AreEqual(true, SqlConnectionOpened);
  25: }

Result: Just Mock was able to create the mock without crashing but when I try to set alternate functionality to the Open method, recording that I called Open, it crashes saying invalid connection string.  My question is why does it care about a connection string when I’m setting behavior, unless I’m missing how to set this up.

Conclusion

As you can see in the examples the syntax is nice and simple, unfortunately Just Mock failed to pass either of my test and to be honest I’m not surprised, it’s in early beta and I tested it against the hardest problems I can think of to solve, something that none of the other mocking frameworks I have looked at even attempt. 

What really impressed me was that while working on this I tweeted about having problems and within a couple of hours @hkosev (Hristo Kosev) from Telerik, who isn’t one of my followers, tweeted me asking what I was doing to get the errors and asked me if I would mind sending him the source for what I was doing so they could solve the problem, without even asking for help, very nice.

As always here is the link to my source code.

1 comment:

Unknown said...

Hi Richard,
Thanks a lot for the detailed blog post and for taking your time to evaluate JustMock.
We managed to reproduce these issues locally and hopefully both problems will be fixed in the next build (to be released next week).
I'll let you know when the build is available.