January 2011 Entries

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!

I am making it a point not to call them resolutions, because the word has become an institution and is beginning to have no meaning. That's why I end up not keeping my resolutions, I think.
So in the spirit of holding myself to my own commitments, I will make a plan and some realistic goals.

1.) Lose weight. Everyone has this on their list, but I am going to be conservative and specific. I currently weigh 393lbs. (yeah, I know). So I want to plan to lose 10lbs per month, that's 1lb. every three days, that shouldn't be difficult if I stick to my diet and exercise plan.
- How do I do this?
    - Diet: vegetarian. Since I already know I have high blood pressure and borderline high cholesterol, a meat-free diet is in order. I was vegan for a little over 2 years in 2006-2008, I think I can handle vegetarian.
    - Exercise: at least 3 times (preferably every day) a week for 30 minutes. It has to be something that gets my heart rate up, or burns in my muscles. I can walk for cardio to start and mild calisthenics (girly push-ups, crunches, etc.).
        - Move: I spend all my time behind the computer. I have recently started to use a slight variation of the Pomodoro Technique (my Pomodoros are 50 minutes instead of 25). During my 10 minutes every hour to answer emails, chats, etc., I will take a few minutes to stretch.
2.) Get my wife pregnant. We've been talking about it for years. Now that she is done with graduate school and I have a great job, now's the time. We'll be the oldest parents in the PTA most likely, but I don't care.
3.) Blog More. Another favorite among bloggers, but I do have about six drafts for blog posts started. The topics are there all I need to do is flesh out the post. This can be the first hour of any computer time I have after work. As soon as I am done exercising, shower and post.
4.) Speak less. Most people want to speak more. I want to concentrate on the places that I enjoy and that can really use the speakers (like local code camps), rather than trying to be some national speaker. I love speaking at conferences, but I need to spend some more time at home if we're going to get pregnant.
5.) Read more. I got a Kindlealt for Christmas and I am loving it so far. I have almost finished Treasure Islandalt, and am getting ready to pick my next book. I will probably read a lot of classics for 2 reasons: (1) they teach deep lessons and (2) most are free for the Kindlealt.
6.) Find my religion. I was raised Southern Baptist, but I want to find my own way. I've been wanting to go to the local Unitarian Church, so I will make a point to go before the end of March. I also want to add a few religious books to my reading list. My boss bought me a copy of Lee Strobel's The Case for Christ: A Journalist's Personal Investigation of the Evidence for Jesusalt , and I have a copy of Bruce Feiler's Abraham: A Journey to the Heart of Three Faiths (P.S.)alt . I will start there.

Seems like a lot now that I spell it out like this. But these are only starters. I am forty years old. I cannot keep living like I am twenty anymore.

So here we go, 2011.