Jasmine spyOn范围功能中断测试

km6*_*zla 2 javascript jasmine angularjs

案件

当我创建一个间谍时rootScope,期望因某种原因而失败.检查一下plunkr并尝试将其评论出来然后倒车看看.

Plunker示例

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);
  });
});
Run Code Online (Sandbox Code Playgroud)

has*_*sin 10

你需要告诉间谍做点什么:

spyOn(rootScope, 'testFn').andCallThrough();
Run Code Online (Sandbox Code Playgroud)

我在这里更新了plnkr:http://plnkr.co/edit/t3ksMtKSI3CEkCtpZ8tI?p = preview

希望这有帮助!

  • 使用spyOn(rootScope,'testFn').and.callThrough(); 对于Jasmine 2 (5认同)