My scenario: a user can click on a "full screen" button which calls Presenter's
setFullScreen() method. I'd like to unit test this method but am not sure how.
If the unit test wasn't required, I would probably do something like this in my
Presenter:
public function setFullScreen() : void {
view.stage.displayState = StageDisplayState.FULL_SCREEN;
// 'view' is a reference to the screen which Presenter looks after
}
However, this is hard to test. If I try this:
[Test]
public function can_set_normal_screen_mode() : void {
var settingsPresenter : SettingsPresenter = new SettingsPresenter(new
SettingsViewStub());
settingsPresenter.setFullScreen;
var view : UIComponent = UIComponent(settingsPresenter.view);
assertTrue(view.stage.displayState == StageDisplayState.FULL_SCREEN);
}
I get null reference error because the view hasn't been added to a display list
yet and therefore stage is null. I don't really want to add the stub view to
the display list because that's not the point of testing a Presenter, is it?
So I thought I would mock view.stage to contain my implementation of Stage but
calling new Stage() or new MySubclassOfStage() fails because Stage can't be
instantiated using the new keyword.
Right now, I am thinking about moving the actual call to stage.displayState =
... to the View itself and just call this method from the presenter. I won't be
able to test if stage's property has been set correctly (or at least not
easily) but I can see no better way at the moment.
Any suggestions?
Thanks,
Borek