The test of fire: Rhino Mocks 3.5 in the real world
I have been waiting on the release for Rhino Mocks 3.5, seeing what kind of feedback I can get from users. I also had another reason, I hadn't had the chance to really give it a good testing, in the only manner that matter, using it to develop production ready software.
Here is my latest test, which I am fairly pleased with.
[TestFixture]
public class WebcastControllerTest
{
private WebcastController controller;
private IRepository<Webcast> repositoryStub;
private StubEngineContext engineContext;
private IUnitOfWork unitOfWorkStub;
private IDisposable disposeGlobalUnitOfWorkRegistration;
[SetUp]
public void Setup()
{
repositoryStub = MockRepository.GenerateStub<IRepository<Webcast>>();
unitOfWorkStub = MockRepository.GenerateStub<IUnitOfWork>();
controller = new WebcastController(repositoryStub)
{
ControllerContext = new ControllerContext{Name = "webcast"},
};
engineContext = new StubEngineContext();
controller.SetEngineContext(engineContext);
disposeGlobalUnitOfWorkRegistration = UnitOfWork.RegisterGlobalUnitOfWork(unitOfWorkStub);
}
[TearDown]
public void TearDown()
{
disposeGlobalUnitOfWorkRegistration.Dispose();
}
[Test]
public void When_index_called_will_send_existing_webcasts_to_view()
{
var webcasts = new List<Webcast>();
repositoryStub.Stub(x => x.FindAll()).Return(webcasts);
controller.Index();
Assert.AreSame(webcasts, controller.PropertyBag["webcasts"]);
}
[Test]
public void When_display_called_will_send_webcast_to_view()
{
var webcast = new Webcast();
controller.Display(webcast);
Assert.AreSame(webcast, controller.PropertyBag["webcast"]);
}
[Test]
public void When_edit_called_with_webcast_will_send_webcast_to_view()
{
var webcast = new Webcast();
controller.Edit(webcast);
Assert.AreSame(webcast, controller.PropertyBag["webcast"]);
}
[Test]
public void When_saving_invalid_webcast_will_redirect_to_edit()
{
var webcast = new Webcast();
controller.Validator.IsValid(webcast);
controller.PopulateValidatorErrorSummary(webcast, controller.Validator.GetErrorSummary(webcast));
controller.Save(webcast);
Assert.Contains(engineContext.MockResponse.RedirectedTo, "edit");
}
[Test]
public void When_saving_invalid_webcast_will_flash_error_summary_and_webcast_in_flash()
{
var webcast = new Webcast();
controller.Validator.IsValid(webcast);
controller.PopulateValidatorErrorSummary(webcast, controller.Validator.GetErrorSummary(webcast));
controller.Save(webcast);
Assert.AreSame(webcast, controller.Flash["webcast"]);
Assert.AreSame(controller.Validator.GetErrorSummary(webcast), controller.Flash["errorSummary"]);
}
[Test]
public void When_saving_valid_webcast_will_redirect_to_display()
{
var webcast = new Webcast();
// explicitly not calling validation, the controller will assume
// that this is a valid object
controller.Save(webcast);
Assert.Contains(engineContext.MockResponse.RedirectedTo, "display");
}
[Test]
public void When_saving_valid_webcast_will_save_and_flash_unit_of_work()
{
var webcast = new Webcast();
// explicitly not calling validation, the controller will assume
// that this is a valid object
controller.Save(webcast);
repositoryStub.AssertWasCalled(x=>x.Save(webcast));
unitOfWorkStub.AssertWasCalled(x=>x.TransactionalFlush());
}
}