question on verifying expect() with arguments...

46 views
Skip to first unread message

asking

unread,
Sep 26, 2012, 4:57:51 PM9/26/12
to mock...@googlegroups.com
Hey, big fan of Mockolate, thanks for your hard work!

Question on expect:

I want to ensure a method on a mock was called with particular data:

Let's say the signature of myMock.myMethod() takes an Array, and I want to ensure that that Array contains ['someString',  {foo:'bar'}].  My problem in asserting is that this Array is written somewhere else, so I can't do:

expect(myMock.myMethod(['someString', {foo:'bar'}])).once();

...because although the 'someString' will pass, the actual {foo:'bar'} associative array that will be in the resulting Array passed to myMock.myMethod() is assembled somewhere else, so effectively it's a different object in memory, etc.  So, is there a softer way of doing so?

Thanks for your time!
adq

Hob Spillane

unread,
Sep 27, 2012, 4:31:14 AM9/27/12
to mock...@googlegroups.com

For more complex verifications, I usually use callsWithInvocatio() like so:

expect(myMock.myMethod().callsWithInvocation(someTestMethod);

function someTestMethod(inn:Invocation):void
{
var arg:Array = inn.arguments[0];
assertEqual('someString', arg[0]);
assertEqual('bar', arg[1].foo);
}

-Hob

asking

unread,
Sep 27, 2012, 11:00:31 AM9/27/12
to mock...@googlegroups.com
Thanks for the reply!

What exactly does callsWithInvocation do? I've set it up as you say, but I noticed while stepping thru while debugging, that I'm not passing through the assertion function, so is this test falsely passing?

function assertDataRecieved(inn:Invocation):void
{
    var args:Array = inn.arguments[0];
    assertThat(arg[0].foo, equalTo(0));
    assertThat(arg[1].bar, equalTo(1));
}

expect(myMock.myMethod).callsWithInvocation(assertDataRecieved);

...so, while running this test, assertDataRecieved() is never called...

Any ideas?
Thanks!

Drew Bourne

unread,
Sep 27, 2012, 11:22:06 AM9/27/12
to mock...@googlegroups.com

Drew Bourne

unread,
Sep 27, 2012, 11:51:14 AM9/27/12
to mock...@googlegroups.com
In your call to expect, you need to invoke the myMock.myMethod with arguments, not pass in the function reference. 

eg: 
     
    expect(myMock.myMethod(args(anything()))).callsWithInvocation(assertDataRecieved);

HTH

asking

unread,
Sep 27, 2012, 4:27:02 PM9/27/12
to mock...@googlegroups.com
Drew, thanks so much for the reply,

Ok, got the expect working with callsWithInvocation, thanks!. I very powerful tool for some unit and functional testing! I was confused because the above example was:

expect(myMock.myMethod().callsWithInvocation(someTestMethod); 

So, Spy is part of what version? I am using:

            <artifactId>mockolate</artifactId>
            <version>0.12.4-as3</version>

And I don't seem to have access to Spy... hmm...is it part of a beta api, should I build from source?

Thanks!

Drew Bourne

unread,
Sep 28, 2012, 11:13:31 AM9/28/12
to mock...@googlegroups.com
I thought the spy support was in that release. If you build from the source then it's definitely in there. 

Run 'ant clean package' after checking out master and you should be good. 

cheers, 
Drew
Reply all
Reply to author
Forward
0 new messages