Geeks With Blogs

News INETA Community Speakers Program

Yes We Kanban

View Lee Brandt's profile on LinkedIn

Addicted toThe Most Intelligent Add-In To Visual Studio


Lee Brandt's Blog You're only as smart as your last line of code

I have been using Rhino.Mocks pretty much since I started being a mockist-type tester. I have been very happy with it for the most part, but a year or so ago, I got a glimpse of some tests using Moq. I thought the little bit I saw was very compelling. For a long time, I had been using:

   1: var _repository = MockRepository.GenerateMock<IRepository>();
   2: _repository.Expect(repo=>repo.SomeCall()).Return(SomeValue);
   3: var _controller = new SomeKindaController(_repository);
   4:  
   5: ... some exercising code
   6: _repository.AssertWasCalled(repo => repo.SomeCall());

I was happy with that syntax. I didn’t go looking for something else, but what I saw was:

   1: var _repository = new Mock();

And I thought, “That looks really nice!” The code was very expressive and easier to read that the Rhino.Mocks syntax. I have gotten so used to the Rhino.Mocks syntax that it made complete sense to me, but to developers I was mentoring in mocking, it was sometimes to obtuse.

SO I thought I would write some tests using Moq as my mocking tool. But I discovered something ugly once I got into it. The way Mocks are created makes Moq very easy to read, but that only gives you a Mock not the object itself, which is what you’ll need to pass to the exercising code. So this is what it ends up looking like:

   1: var _repository = new Mock<IRepository>();
   2: _repository.SetUp(repo=>repo.SomeCall).Returns(SomeValue);
   3: var _controller = new SomeKindaController(_repository.Object);
   4: .. some exercizing code
   5: _repository.Verify(repo => repo.SomeCall());

Two things jump out at me: 1) when I set up my mocked calls, do I set it on the Mock or the Mock’s “object”? and 2) What am I verifying on SomeCall? Just that it was called? that it is available to call? Dealing with 2 objects, a “Mock” and an “Object” made me have to consider naming conventions. Should I always call the mock _repositoryMock and the object _repository? So I went back to Rhino.Mocks. It is the most widely used framework, and show other how to use it is easier because there is one natural object to use, the _repository.

Then I came across a blog post from Patrik Hägne, and that led me to a post about FakeItEasy. I went to the Google Code site and when I saw the syntax, I got very excited. Then I read the wiki page where Patrik stated why he wrote FakeItEasy, and it mirrored my own experience. So I began to play with it a bit. So far, I am sold. the syntax is VERY easy to read and the fluent interface is super discoverable. It basically looks like this:

   1: var _repository = A.Fake<IRepository>();
   2: a.CallTo(repo=>repo.SomeMethod()).Returns(SomeValue);
   3: var _controller = new SomeKindaController(_repository);
   4: ... some exercising code
   5: A.CallTo(() => _repository.SOmeMethod()).MustHaveHappened();

Very nice. But is it mature? It’s only been around a couple of years, so will I be giving up some thing that I use a lot because it hasn’t been implemented yet? I doesn’t seem so. As I read more examples and posts from Patrik, he has some pretty complex scenarios. He even has support for VB.NET!

So if you are looking for a mocking framework that looks and feels very natural, try out FakeItEasy!

Posted on Thursday, January 13, 2011 10:58 PM | Back to top


Comments on this post: Fake It Easy On Yourself

# re: Fake It Easy On Yourself
Requesting Gravatar...
I have also become a FakeItEasy addict. Thanks for the comparison.
By the way, could you make the comment box somewhat bigger?
Left by Rainer Hilmer on Jan 14, 2011 3:20 AM

# re: Fake It Easy On Yourself
Requesting Gravatar...
Hey Rainer:

Thanks for the feedback.

~L
Left by Lee Brandt on Jan 14, 2011 7:28 AM

# re: Fake It Easy On Yourself
Requesting Gravatar...
Since you're looking around, you ought to give NSubstitute a try.
Left by Mike Murray on Jan 14, 2011 9:44 AM

# re: Fake It Easy On Yourself
Requesting Gravatar...
[to developers I was mentoring in mocking, it was sometimes to obtuse.]

Just to be pedantic, I think you mean "abstruse"!

Left by AT on Dec 09, 2011 11:53 AM

# re: Fake It Easy On Yourself
Requesting Gravatar...
Have you tried Typemock Isolator????
Left by Ron on Jun 20, 2012 9:35 AM

Your comment:
 (will show your gravatar)


Copyright © Lee Brandt | Powered by: GeeksWithBlogs.net