Jasmine spyOn范围功能中断测试

时间:2014-02-06 22:53:28

标签: javascript angularjs jasmine

案例

当我在rootScope上创建间谍时,由于某种原因,期望失败。检查一下plunkr,然后尝试将其注释掉并反转以查看。

代码

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);
  });
});

1 个答案:

答案 0 :(得分:10)

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

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

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

希望这有帮助!