7 July 2008

Mocking Rhinos...

by mo


I mean Grokking Rhino Mocks. My usage of Rhino Mocks has changed quite a bit since I first started using it a year ago, as well as the way I write tests.

First it was like this:

  [TestFixture]
  public class ConsoleTest {
      private MockRepository mockery;
      private IReportPresenter presenter;
  
      [SetUp]
      public void SetUp() {
          mockery = new MockRepository();
          presenter = mockery.CreateMock<IReportPresenter>();
      }
  
      [TearDown]
      public void TearDown() {
          mockery.VerifyAll();
      }
  
      [Test]
      public void ShouldInitializeTheReportPresenter() {
          var commandLineArguments = new[] {"blah"};
          presenter.Initialize();
  
          mockery.ReplayAll();
          new Console(presenter).Execute(commandLineArguments);
      }
  }

Then it evolved to this…

  [TestFixture]
  public class ConsoleTest {
      private MockRepository mockery;
      private IReportPresenter presenter;
  
      [SetUp]
      public void SetUp() {
          mockery = new MockRepository();
          presenter = mockery.DynamicMock<IReportPresenter>();
      }
  
      [Test]
      public void ShouldInitializeTheReportPresenter() {
          var commandLineArguments = new[] {"blah"};
  
          using (mockery.Record()) {
              presenter.Initialize();
          }
          using (mockery.Playback()) {
              CreateSUT().Execute(commandLineArguments);
          }
      }
  
      private IConsole CreateSUT() {
          return new Console(presenter);
      }
  }

Then for a short time I tried this…

  [TestFixture]
  public class when_giving_the_console_valid_arguments {
      private IReportPresenter presenter;
  
      [SetUp]
      public void SetUp() {
          presenter = MockRepository.GenerateMock<IReportPresenter>();
      }
  
      [Test]
      public void should_initialize_the_report_presenter() {
          var commandLineArguments = new[] {"blah"};
          CreateSUT().Execute(commandLineArguments);
          presenter.AssertWasCalled(p => p.Initialize());
      }
  
      private IConsole CreateSUT() {
          return new Console(presenter);
      }
  }

Now I’m trying this…

  [Concern(typeof (Console))]
  public class when_the_console_is_given_valid_console_arguments : context_spec<IConsole> {
      private string[] command_line_arguments;
      private IReportPresenter presenter;
  
      protected override IConsole UnderTheseConditions() {
          command_line_arguments = new[] {"path", "testfixtureattributename"};
          presenter = Dependency<IReportPresenter>();
  
          return new Console(presenter);
      }
  
      protected override void BecauseOf() {
          sut.Execute(command_line_arguments);
      }
  
      [Test]
      public void should_initialize_the_report_presenter() {
          presenter.should_have_been_asked_to(p => p.Initialize());
      }
  }

Now I’m generating reports from my ~tests~ specs using this.

books csharp tdd