Asserting the contents of mocked object argument.

59 views
Skip to first unread message

matej matej

unread,
Dec 15, 2012, 8:39:20 AM12/15/12
to mock...@googlegroups.com
Hello,

I am learning mockolate and unit testing, so if you can help me please :)

In my mediator in RLegs2 I have following code.

This is a function that triggers on MouseEvent from View.

private function onLogin(event:MouseEvent):void {
                    var userInfo:UserInfo = new UserInfo(view.usernameField.text,view.passwordField.text);
                    authorizationSignal.dispatch(userInfo);

                }

Now in my test i have mocked the view to return username and password, and I have mocked the signal.

How can I check if the userInfo argument from mocked signal dispatch method contains the correct data?

Thanks in advance guys!


Drew Bourne

unread,
Dec 18, 2012, 3:38:10 PM12/18/12
to mock...@googlegroups.com
1) Use an Expectation with a very specific matcher:

    expect(authorizationSignal.dispatch(arg(allOf(instanceOf(UserInfo), hasProperties({ username: "test username", password: "test password" })))).once();

2) Use a Spy

    var authSpy:Spy = spy(authorizationSignal.dispatch(arg(anything()));

    // trigger mouse-event / onLogin() then check the spy

    assertTrue("dispatch was called", authSpy.calledOnce());
    assertTrue("dispatched UserInfo", authSpy.calledWith(instanceOf(UserInfo)));
    assertTrue("dispatched with username & password", 
    authSpy.calledWith(hasProperties({ username: "test username": password: "test password" })));

HTH,
Drew

matej matej

unread,
Dec 19, 2012, 1:41:22 AM12/19/12
to mock...@googlegroups.com
Thank you Drew
Reply all
Reply to author
Forward
0 new messages