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
.
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.
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").
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With