withArgs() on a spy created with createSpyObj()

3,451 views
Skip to first unread message

jason....@blackbaud.com

unread,
May 14, 2019, 1:48:53 PM5/14/19
to Jasmine
I'm creating a spy with createSpyObj():

mockAccessResolverService = jasmine.createSpyObj('AccessResolverService', ['resolveAccessByEntitlements', 'resolveAccess']);

I use createSpyObj() because I need to configure it as a provider in my test bed.

In my tests I want to provide the behavior for when the mocked methods are called, but I want different behavior depending on how the methods are called. If I had used spyOn() I could use withArgs() but it doesn't seem possible with a spy created with createSpyObj():

mockAccessResolverService.resolveAccess.withArgs([DELETE_PERMISSION]).and.returnValue(Observable.of(false));

TypeError: mockAccessResolverService.resolveAccess.withArgs is not a function


Is it possible to create a spy with createSpyObj() and return different values based on the passed arguments?

Gregg Van Hove

unread,
May 14, 2019, 8:37:38 PM5/14/19
to jasmi...@googlegroups.com
From Jasmine's perspective, what you want to do is change the behavior of the spy after creating it. (Spies default to the `stub` strategy). In order to do this you'll need to use the `.and` chaining property to get back to spy things. This might look like:

mockAccessResolverService.resolveAccess.and.withArgs([DELETE_PERMISSION]).and.returnValue(Observable.of(false));

Hope this helps. Thanks for using Jasmine!


--
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+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at https://groups.google.com/group/jasmine-js.
To view this discussion on the web visit https://groups.google.com/d/msgid/jasmine-js/afd3e27e-220d-4599-9187-6b74a2cf357d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jason Bodnar

unread,
May 28, 2019, 5:43:01 PM5/28/19
to Jasmine
I changed my code to:

mockAccessResolverService.resolveAccess.and.withArgs(AUTH_PERMISSIONS.DELETE).and.returnValue(Observable.of(false));

And I get:

TypeError: mockAccessResolverService.resolveAccess.and.withArgs is not a function


The docs (https://jasmine.github.io/api/edge/Spy) show withArgs() called directly on the Spy:

spyOn(someObj, 'func').withArgs(1, 2, 3).and.returnValue(42);

So I tried:

mockAccessResolverService.resolveAccess.withArgs(AUTH_PERMISSIONS.DELETE).and.returnValue(Observable.of(false));


and I get:

TypeError: mockAccessResolverService.resolveAccess.withArgs is not a function




On Tuesday, May 14, 2019 at 7:37:38 PM UTC-5, Gregg Van Hove wrote:
From Jasmine's perspective, what you want to do is change the behavior of the spy after creating it. (Spies default to the `stub` strategy). In order to do this you'll need to use the `.and` chaining property to get back to spy things. This might look like:

mockAccessResolverService.resolveAccess.and.withArgs([DELETE_PERMISSION]).and.returnValue(Observable.of(false));

Hope this helps. Thanks for using Jasmine!


On Tue, May 14, 2019 at 10:48 AM <jason...@blackbaud.com> wrote:
I'm creating a spy with createSpyObj():

mockAccessResolverService = jasmine.createSpyObj('AccessResolverService', ['resolveAccessByEntitlements', 'resolveAccess']);

I use createSpyObj() because I need to configure it as a provider in my test bed.

In my tests I want to provide the behavior for when the mocked methods are called, but I want different behavior depending on how the methods are called. If I had used spyOn() I could use withArgs() but it doesn't seem possible with a spy created with createSpyObj():

mockAccessResolverService.resolveAccess.withArgs([DELETE_PERMISSION]).and.returnValue(Observable.of(false));

TypeError: mockAccessResolverService.resolveAccess.withArgs is not a function


Is it possible to create a spy with createSpyObj() and return different values based on the passed arguments?

--
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 jasmi...@googlegroups.com.

Bodnar, Jason

unread,
May 28, 2019, 5:46:32 PM5/28/19
to Jasmine
The issue was that while the typing js for typescript we're using is for jasmine 3.0 we were actually still using a jasmine 2.x lib. So my ide thought it was okay but the runtime blew up.

To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.

To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at https://groups.google.com/group/jasmine-js.

For more options, visit https://groups.google.com/d/optout.


--

Gregg Van Hove

unread,
May 31, 2019, 9:01:01 PM5/31/19
to jasmi...@googlegroups.com
Glad you figured it out. The upgrade from Jasmine 2.x to 3.x should not be anywhere nearly as bad as the upgrade from 1.x to 2.x, so I would encourage you to update if you can, for this feature and many more. I'm not sure at which point the DefinitelyTyped typings switched over to Jasmine 3.0, but Jasmine 3.0 was released over a year ago.

Reply all
Reply to author
Forward
0 new messages