Sunday, June 13, 2010

Rhino Mocks moving to the AAA syntax

Having looked at how I should write my unit tests, I've started to move to the AAA syntax (Arrange, Act, Assert). I found it quite hard (me being stupid) to find a before and after example - so here's my before and after...

// Art of unit testing (7.3.1 naming unit tests)
  //  name of method being tested _ scenario being tested _ the expected behaviour
  //  MethodUnderTest_Scenario_Behaviour
  [TestMethod]
  public void CreateParser_WellKnownDataset_PostsNodesToReciever()
  {
   MockRepository mocks = new MockRepository();
   IParsedReceiver receiver = mocks.StrictMock<IParsedReceiver>();

   using (mocks.Record())
   {
    receiver.AcceptNode(null);
    LastCall.IgnoreArguments().Repeat.Times(14);
   }

   Parser parser = Manager.CreateParser(receiver, @"..\..\ParseData\OsmTestData.xml");

   mocks.VerifyAll();
  }

and using the 3.5 Arrange, Act, Assert approach:

// Art of unit testing (7.3.1 naming unit tests)
  //  name of method being tested _ scenario being tested _ the expected behaviour
  //  MethodUnderTest_Scenario_Behaviour
  [TestMethod]
  public void CreateParser_WellKnownDataset_PostsNodesToReciever_AsStub()
  {
   // arrange
   IParsedReceiver receiver = MockRepository.GenerateStub<IParsedReceiver>();

   // act
   Parser parser = Manager.CreateParser(receiver, @"..\..\ParseData\OsmTestData.xml");
   
   // assert
   receiver.AssertWasCalled(c => c.AcceptNode(null), options => options.Repeat.Times(14).IgnoreArguments());
  }

which has the nice side effect of being less code as well!

No comments: