Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine spyOn scope function breaks test

Case

When I create a spy on rootScope, the expectation fails for some reason. Check out the plunkr and try just commenting it out and reversing to see.

Code

Plunker Example

describe('Testing', function() {
  var rootScope = null

  //you need to indicate your module in a test
  // beforeEach(module('plunker'));

  beforeEach(inject(function($rootScope, $controller) {
    rootScope = $rootScope;

    rootScope.value = false;

    rootScope.testFn = function() {
      rootScope.value = true;
    }
  }));

  it('should modify root scope', function() {
    // Creating this spy makes test fail
    // Comment it outto make it pass
    spyOn(rootScope, 'testFn');
    expect(rootScope.value).toEqual(false);
    rootScope.testFn();
    expect(rootScope.value).toEqual(true);
  });
});
like image 529
km6zla Avatar asked Mar 26 '26 18:03

km6zla


1 Answers

You need to tell the spy to do something:

spyOn(rootScope, 'testFn').andCallThrough();

I updated the plnkr here: http://plnkr.co/edit/t3ksMtKSI3CEkCtpZ8tI?p=preview

Hope this helped!

like image 162
hassassin Avatar answered Mar 29 '26 06:03

hassassin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!