Tuesday, February 24, 2009

Presenter Tests 101

What to wrtite a test to make sure the prsenter load dats from a service/moeld/repository to the view?

IFooEditView view ;
IFooService service ;

[TestInitialize]
public void MyTestInitialize()
{
view = MockRepository.GenerateMock<IFooEditView>();
service = MockRepository.GenerateMock<IFooService>();
}

[TestCleanup]
public void MyTestCleanup()
{
//global assertion
view.VerifyAllExpectations();
service.VerifyAllExpectations();
}

[TestMethod]
public void CanInitialiseFooEditPresenter()
{
//arrange
var id = 1;
var record = new Foo(id);
view.Expect(v => v.LoadRecord(record));
service.Expect(s => s.RetrieveFooRecord(id)).Return(record);
//act
var pres = new FooEditPresenter(view, service);
pres.Initialize(id);
//assert - mock verification in tear down
}

NB: I really should stop posting code by writing staight in to the blogger create post screen. just lazy...

No comments: