This is just a basic post about the Rhino Mocks AAA Syntax. Hopefully I will cover more on the AAA syntax in the future posts. Let's first see how we can test a view by using the old method of Record/Replay/Playback. This will be a very simple unit test. We are going to test that if the AddCustomer method of the CustomerRepository returns "true" then the view message is set to "Success".
Here is what the unit test looks like:
[TestFixture]
public class when_the_customer_is_added
{
private AddCustomerPresenter _presenter;
private ICustomerRepository _repository;
private MockRepository _mocks;
private IAddCustomerView _view;
[SetUp]
public void initialize()
{
_mocks = new MockRepository();
_view = _mocks.DynamicMock<IAddCustomerView>();
}
[Test]
public void should_display_the_success_message()
{
var repositoryStub = _mocks.Stub<ICustomerRepository>();
_presenter = new AddCustomerPresenter(_view, repositoryStub);
using(_mocks.Record())
{
SetupResult.For(repositoryStub.AddCustomer(null)).IgnoreArguments().Return(true);
SetupResult.For(_view.FirstName).Return("Mohammad");
SetupResult.For(_view.LastName).Return("Azam");
Expect.Call(_view.Message = "Success");
}
using(_mocks.Playback())
{
_presenter.AddCustomer();
}
}
}
The above test will pass correctly without any problems. Now, let's see how we can achieve the same result using the Rhino Mocks AAA Syntax.
[TestFixture]
public class when_the_customer_is_added
{
private AddCustomerPresenter _presenter;
private ICustomerRepository _repository;
private IAddCustomerView _view;
[SetUp]
public void initialize()
{
_view = MockRepository.GenerateMock<IAddCustomerView>();
}
[Test]
public void should_display_the_success_message()
{
var repositoryStub = MockRepository.GenerateStub<ICustomerRepository>();
_presenter = new AddCustomerPresenter(_view, repositoryStub);
repositoryStub.Stub(x => x.AddCustomer(null)).IgnoreArguments().Return(true);
_view.Stub(x => x.FirstName).Return("Mohammad");
_view.Stub(x => x.LastName).Return("Azam");
_presenter.AddCustomer();
_view.AssertWasCalled(x => x.Message = "Success");
}
}
Our code is reduced a bit and now we are using the power of the Lambda Expressions. One important thing to note is that you will have to use the MockRepository.GenerateMock and MockRepository.GenerateStub to create your mock and stub objects.
You should also note that I am using _view.Stub instead of _view.Expect for FirstName and LastName properties. The main purpose of _view.Stub is to provide the dummy values to the unit test when needed without failing the unit test. If you replace them with _view.Expect and then change the presenter AddCustomer method to the following then the test will fail.
public void AddCustomer()
{
var customer = new Customer() { FirstName = "Mohammad", LastName = _view.LastName };
var result =_repository.AddCustomer(customer);
if(result)
{
_view.Message = "Success";
}
}
// And here is the unit test
[Test]
[Test]
public void should_display_the_success_message()
{
var repositoryStub = MockRepository.GenerateStub<ICustomerRepository>();
_presenter = new AddCustomerPresenter(_view, repositoryStub);
repositoryStub.Stub(x => x.AddCustomer(null)).IgnoreArguments().Return(true);
_view.Expect(x => x.FirstName).Return("Mohammad");
_view.Stub(x => x.LastName).Return("Azam");
_presenter.AddCustomer();
_view.AssertWasCalled(x => x.Message = "Success");
}
The test fails because now it is expecting a call on FirstName which should return "Mohammad". But there is no call in the presenter's code and hence the test fails.
The last line is _view.VerifyAllExpectations which will verify that the expectations set on the view were all met. If any of the expectation was not met then the test will fail.
This is just the basic introduction to the Rhino Mocks AAA Syntax. Stay tuned for more posts!