Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change return value of jasmine spy?

I'm using Jasmine to create a spy like so:

beforeEach(inject(function ($injector) {     $rootScope = $injector.get('$rootScope');     $state = $injector.get('$state');     $controller = $injector.get('$controller');      socket = new sockMock($rootScope);      //this is the line of interest     authService = jasmine.createSpyObj('authService', ['login', 'logout', 'currentUser']); })); 

I'd like to be able to change what's returned by the various methods of authService.

Here are how the actual tests are set up:

function createController() {     return $controller('UserMatchingController', {'$scope': $rootScope, 'socket':socket, 'authService': authService }); }  describe('on initialization', function(){     it('socket should emit a match', function() {         createController();          expect(socket.emits['match'].length).toBe(1);     });      it('should transition to users.matched upon receiving matched', function(){          //this line fails with "TypeError: undefined is not a function"         authService.currentUser.andReturn('bob');          createController();          $state.expectTransitionTo('users.matched');         socket.receive('matchedblah', {name: 'name'});          expect(authService.currentUser).toHaveBeenCalled()     }) }) 

Here's how the controller is set up:

lunchrControllers.controller('UserMatchingController', ['$state', 'socket', 'authService',     function ($state, socket, authService) {         socket.emit('match', {user: authService.currentUser()});          socket.on('matched' + authService.currentUser(), function (data) {             $state.go('users.matched', {name: data.name})         });     }]); 

Essentially, I'd like to be able to change the return value of spied methods. However, I'm not sure if I'm correctly approaching the problem by using jasmine.createSpyObj.

like image 658
Paymahn Moghadasian Avatar asked Mar 06 '15 00:03

Paymahn Moghadasian


People also ask

How do you use parameters in spyOn method?

spyOn() takes two parameters: the first parameter is the name of the object and the second parameter is the name of the method to be spied upon. It replaces the spied method with a stub, and does not actually execute the real method. The spyOn() function can however be called only on existing methods.

How do you spy a variable in Jasmine?

In Jasmine, you can do anything with a property spy that you can do with a function spy, but you may need to use different syntax. Use spyOnProperty to create either a getter or setter spy. it("allows you to create spies for either type", function() { spyOnProperty(someObject, "myValue", "get").

How do you get rid of Jasmine spy?

if are already spying on a method and you want the original method to be called instead you should call andCallThrough() which will override the first spy behavior. This is imho the best solution. I prefer to say that setting isSpy to false is a bad thing to do.

How do you mock a function in Jasmine?

Using Jasmine spies to mock code Jasmine spies are easy to set up. You set the object and function you want to spy on, and that code won't be executed. In the code below, we have a MyApp module with a flag property and a setFlag() function exposed. We also have an instance of that module called myApp in the test.


1 Answers

Try this instead. The API changed for Jasmine 2.0:

authService.currentUser.and.returnValue('bob'); 

Documentation:

http://jasmine.github.io/2.0/introduction.html#section-Spies

like image 88
sma Avatar answered Sep 20 '22 18:09

sma