FF5Ninja FF5Ninja - 1 year ago 61
AngularJS Question

What type of spy to use for testing

I know when you use

you can have different forms like
. I'm not really sure which one I need for this code I'm trying to test...

var lastPage = $cookies.get("ptLastPage");
if (typeof lastPage !== "undefined") {
} else {
$location.path('/home'); //TRYING TO TEST THIS ELSE STATEMENT

Here is some of my test code:

describe('Spies on cookie.get', function() {
beforeEach(inject(function() {
spyOn(cookies, 'get').and.callFake(function() {
return undefined;
it("should work plz", function() {

I've tried a lot of different things, but I'm trying to test the
statement. Therefore I need to make
cookies.get == undefined
Everytime I try to do that though, I get this error:

Expected '' to be '/home'.

The value of
never changes when
is equal to
. I think I'm using the spyOn incorrectly?

Follow-up on my mock values:

function(_$location_, _$route_, _$rootScope_, _$cookies_) {
location = _$location_;
route = _$route_;
rootScope = _$rootScope_;
cookies = _$cookies_;

Follow-up on the functions:

angular.module('buildingServicesApp', [
.config(function($routeProvider) {
.run(function($rootScope, $location, $http, $cookies)

No names on these functions, therefore how do I call the

Answer Source

Right now, you're testing that the location.path() function works as designed. I'd say you should leave that testing to the AngularJS team :). Instead, verify that the function was called correctly:

  describe('Spies on cookie.get', function() {
    beforeEach((function() { // removed inject here, since you're not injecting anything
      spyOn(cookies, 'get').and.returnValue(undefined); // As @Thomas noted in the comments
      spyOn(location, 'path');
    it("should work plz", function() {
      // cookies.get(); replace with call to the function/code which calls cookies.get()

Note that you shouldn't be testing that your tests mock cookies.get, you should be testing that whatever function calls the first bit of code in your question is doing the right thing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download