Override spy return value in nested describe

18,905 views
Skip to first unread message

Steve Horn

unread,
Jan 7, 2011, 11:26:05 AM1/7/11
to Jasmine
I have a beforeEach in a describe that creates a spy via:
var someSpy = jasmine.createSpy('SomeObject');.

Then I set up a stub for a method on that object:
someSpy.some_method =
jasmine.createSpy("some_method").andReturns("FOO");

That works fine and all, but the rub comes in when I want to reference
the same spyed-upon object in a describe context nested within the
describe I mentioned above.

Instead of returning "FOO" in my nested describe, I want it to return
"BAR". I tried simply re-declaring the spy, but I get a error from
Jasmine:
"Error: some_method has already been spied upon "

Is there any way for me to redefine the return value for a spy in a
nested describe?

Corey Haines

unread,
Jan 7, 2011, 12:06:03 PM1/7/11
to jasmi...@googlegroups.com
Can you reset the spy and set it up again?

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

--
http://www.coreyhaines.com
The Internet's Premiere source of information about Corey Haines

Rajan Agaskar

unread,
Jan 7, 2011, 12:14:05 PM1/7/11
to jasmi...@googlegroups.com
Apologies if there's syntax goofs -- it's been awhile: 

var spy = jasmine.createSpy("spyname").andReturn("foo")
someObj.method = spy;
expect(someObj.method()).toEqual("foo");
spy.andReturn("bar");
expect(someObj.method()).toEqual("bar");

You could also, as Corey suggests, reset the spy, but keep in mind that will reset callCounts, etc (this may be what you want). 

If you're spying on a defined object, consider: 

spyOn(someObj, "method").andReturn("foo");
expect(someObj.method()).toEqual("foo");
someObj.method.andReturn("bar");
expect(someObj.method()).toEqual("bar");

Thanks!

Rajan

Steve Horn

unread,
Feb 15, 2011, 11:00:00 AM2/15/11
to Jasmine
Corey/Rajan
Thanks for the tip, "resetting" the spy worked.

Insetad of recreating the spy:
spyOn(object, 'methodName').andReturn("foo");

In the nested describe change the return value on the existing spy:
object.methodName.andReturn("bar");

Not as fluent (since you lose the "spyOn" method call), but it works.
> > jasmine-js+...@googlegroups.com<jasmine-js%2Bunsubscribe@googlegrou ps.com>
> > .
> > > For more options, visit this group at
> >http://groups.google.com/group/jasmine-js?hl=en.
>
> > --
> >http://www.coreyhaines.com
> > The Internet's Premiere source of information about Corey Haines
>
> > --
> > 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%2Bunsubscribe@googlegrou ps.com>
> > .

Ka Mok

unread,
Jan 10, 2017, 1:10:30 PM1/10/17
to Jasmine
This no longer works for Jasmine 2.5. Is there an alternative? 

Gregg Van Hove

unread,
Jan 11, 2017, 7:38:01 PM1/11/17
to jasmi...@googlegroups.com
Based on this email, I can't tell what functionality you're looking for. A couple of things though:

1. If you're spying on a method on an object, you should probably use `spyOn` as it will be cleaned up after each spec. `createSpy` is intended for other use cases.

2. If you want to change the behavior of the spy, you can call `.and.<whichever>` again at a later date on the same spy object.

If you want more details, take a look at the docs as well: https://jasmine.github.io

Hope this helps. Thanks for using Jasmine!

- Gregg

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+unsubscribe@googlegroups.com.

To post to this group, send email to jasmi...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages