toHaveBeenCalledWith / checking a specific parameter

19,365 views
Skip to first unread message

fschwiet

unread,
Jul 31, 2010, 4:22:41 AM7/31/10
to Jasmine
I've been looking at Jasmine for the first time, I am impressed. It
is doing all the things I'd like to do from a test runner and mocking
perspective.

I wonder about checking the parameters passed to a spy. I might use
expect(some.method).toHaveBeenCalledWith(1,2,3) to verify some's
method was called with (1,2,3). What if I want to check just one of
those parameters though, and ignore the rest?

The only way I can see to do this is with a callback:

spyOn(some, 'method').andCallFake(first,second,third) {
expect(second).toEqual(2);
});

I wonder if there is a better way. This way seems non-idiomatic for
Jasmine since the verification is specified during setup.

Rajan Agaskar

unread,
Jul 31, 2010, 12:08:19 PM7/31/10
to jasmi...@googlegroups.com
I believe you can use

expect(some.method).toHaveBeenCalledWith(jasmine.any(), 2, jasmine.any());

I also think we permit jasmine any to take a class, so if those other
args should always be numbers, you can

expect(some.method).toHaveBeenCalledWith(jasmine.any(Number), 2,
jasmine.any(Number));

Yet another method which we've often used for Ajax mocks (when not
using a dedicated mock -- preferred, in this case) is:

var args = some.method.mostRecentCall().args
expect(args[1]).toEqual(2)

Apologies if any of this syntax is a bit off -- don't have the jasmine
env loaded in front of me at the moment. Hope this helps!

Rajan

> --
> 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.
>
>

fschwiet

unread,
Jul 31, 2010, 2:34:40 PM7/31/10
to Jasmine
Excellent.

Porting a bunch tests over to Jasmine at the moment. Jasmine is
making me drool all my tests get shorter and cleaner.

On Jul 31, 9:08 am, Rajan Agaskar <ra...@pivotallabs.com> wrote:
> I believe you can use
>
> expect(some.method).toHaveBeenCalledWith(jasmine.any(), 2, jasmine.any());
>
> I also think we permit jasmine any to take a class, so if those other
> args should always be numbers, you can
>
> expect(some.method).toHaveBeenCalledWith(jasmine.any(Number), 2,
> jasmine.any(Number));
>
> Yet another method which we've often used for Ajax mocks (when not
> using a dedicated mock -- preferred, in this case) is:
>
> var args = some.method.mostRecentCall().args
>  expect(args[1]).toEqual(2)
>
> Apologies if any of this syntax is a bit off -- don't have the jasmine
> env loaded in front of me at the moment. Hope this helps!
>
> Rajan
>

Davis Frank

unread,
Jul 31, 2010, 2:43:13 PM7/31/10
to jasmi...@googlegroups.com
That was among the intent.  Good luck and we look forward to your feedback!

thx,
--dwf

fschwiet

unread,
Jul 31, 2010, 8:00:33 PM7/31/10
to Jasmine
Well I just converted a bunch of tests from a small project,
everything is handled and most things are handled better. I'm glad I
found this as I'm starting up a significant JS project soon. One
thing I wonder though, sometimes I check that a result is a particular
type. The shortest way I've found to do this is

expect(foo.bar).toEqual(jasmine.any(Function));

This verifies foo.bar is a function) Is there a cleaner way to do
this? I feel like adding

expect(foo.bar).toBeA(Function)

but I wonder if I've overlooked something.



On Jul 31, 11:43 am, Davis Frank <dwfr...@pivotallabs.com> wrote:
> That was among the intent.  Good luck and we look forward to your feedback!
>
> thx,
> --dwf
>
> > jasmine-js+...@googlegroups.com<jasmine-js%2Bunsu...@googlegroups.com>
> > .
> > > > For more options, visit this group athttp://
> > groups.google.com/group/jasmine-js?hl=en.
>
> > --
> > 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<jasmine-js%2Bunsu...@googlegroups.com>
> > .

Davis Frank

unread,
Jul 31, 2010, 8:21:13 PM7/31/10
to jasmi...@googlegroups.com
When I use this pattern, I go with your first example.

--dwf

To unsubscribe from this group, send email to jasmine-js+...@googlegroups.com.

Christian Williams

unread,
Jul 31, 2010, 9:06:29 PM7/31/10
to jasmi...@googlegroups.com
I think we have a story in the backlog to add a toBeInstanceOf() matcher.

If you want to make one, we'll pull it in.

--X [typos courtesy of my iPhone]

fschwiet

unread,
Aug 1, 2010, 5:55:04 PM8/1/10
to Jasmine
Alright, toBeInstanceOf() was added. Not sure who to target the pull
request at, so I'll just give the commit link here. I also added
support for jasmine.any(Boolean):


http://github.com/fschwiet/jasmine/commit/f3ec705a470d91f520f9d12c1c1760118e083098
Reply all
Reply to author
Forward
0 new messages