Mocking Rhinos...
Posted on July 07, 2008 @ 03:07 books csharp tddI 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[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...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[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...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[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...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[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. I wonder what's next...