2009/4/9 Jeudy, Guillaume <
gjeudy@...>:
> Thanks for your input Nat,
>
> However it's not clear to me how Greg can apply your technique to his usecase.
>
...
>
> Nat, let me know if i'm off the tracks here.
Yes, you've got the relationships the wrong way round I think.
The presenter calls addListener on the view, passing to the view some
kind of listener interface that the view will call back to.
We're mocking the view, so our test will have to call back to that
interface. We will have to grab hold of it when the presenter calls
our mocked addListener interface. We can do that with a custom
Action.
Then, to invoke whatever the presenter does in response to view
events, we will call method(s) of the listener interface captured by
our custom action.
We then don't care how the presenter handles the notification from the
view, as long as it produces the *externally visible* behaviour we
want (that is, results in a call to the view's creditCardAuthenticated
method). The fact that the presenter has a run() method and whatnot
is an implementation detail that our test should not be concerned
with.
I'd imagine a custom action for this would look something like:
public class AddListenerAction<T extends EventListener> {
private T listener = null;
@SuppressWarnings("unchecked")
public void invoke(Invocation i) {
listener = (T)i.getParameter(0);
}
public T notify() {
if (listener == null) Assert.fail("no listener has been added");
return listener;
}
}
And a test would look something like:
final AddListenerAction<ViewListener> registerListener =
new AddListenerAction<ViewListener>();
context.checking(new Expectations() {{
oneOf(view).addListener(with(any(ViewListener.class));
will(registerListener);
oneOf (viewMock).creditCardAuthenticated(true);
}});
Presenter presenter = new Presenter(view); // this adds the listener
registerListener.notify().someViewEventHere(); // this calls it.
}
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email