Testing for anonymous function in toBeCalledWith

3,469 views
Skip to first unread message

Ben Loveridge

unread,
Jan 13, 2011, 12:39:27 PM1/13/11
to Jasmine
Hi,

In one of my functions, I am invoking an async API which expects a few
parameters, with the last one being a callback function. Due to some
variables that need to be available in the callback function, I am
using an anonymous function as the callback so the callback has scope
reference to those variables.

When testing this function, I am trying to verify that the
asynchronous API is called with the correct parameters -- since the
callback provided is an anonymous function, I can't seem to do it.

expect(the.function).toHaveBeenCalledWith(a, b, c); // doesnt work,
because the 4th parameter was a function which we didn't tell Jasmine
about

Short of writing a custom matcher, is there a way to expect a
parameter to just be anything of a given type? Something like this
comes to mind:

expect(the.function).toHaveBeenCalledWith(a, b, c,
jasmine.typeof("function"));

Thanks in advance for any help or guidance.

- Ben

Rajan Agaskar

unread,
Jan 13, 2011, 12:52:09 PM1/13/11
to jasmi...@googlegroups.com
Look into jasmine.any()

You should be able to

expect(the.function).toHaveBeenCalledWith(a, b, c, jasmine.any(Function));

Thanks!


--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To post to this group, send email to jasmi...@googlegroups.com.
To unsubscribe from this group, send email to jasmine-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jasmine-js?hl=en.


Ben Loveridge

unread,
Jan 13, 2011, 12:55:21 PM1/13/11
to Jasmine
Awesome, that's exactly what I was looking for -- can't believe I
missed that in the JSDoc.

Thanks, and thanks for the fast reply!

- Ben


On Jan 13, 10:52 am, Rajan Agaskar <ragas...@gmail.com> wrote:
> Look into jasmine.any()
>
> You should be able to
>
> expect(the.function).toHaveBeenCalledWith(a, b, c, jasmine.any(Function));
>
> Thanks!
>
> > jasmine-js+...@googlegroups.com<jasmine-js%2Bunsubscribe@googlegrou ps.com>
> > .

Ben Loveridge

unread,
Jan 13, 2011, 12:57:03 PM1/13/11
to Jasmine
And, thanks for not just responding with "RTFM" -- I do feel quite
sheepish though *blush*

Rajan Agaskar

unread,
Jan 13, 2011, 1:10:12 PM1/13/11
to jasmi...@googlegroups.com
Don't ever feel bad about that -- there's lots of room for improvement in our docs. Plenty of patterns some of us use are not well-documented. We'll get there one day. ;) 

Thanks!

Rajan

To unsubscribe from this group, send email to jasmine-js+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages