案例
当我在 rootScope
上创建一个 spy 时,期望由于某种原因失败了。查看 plunkr 并尝试将其注释掉并反向查看。
代码
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);
});
});
最佳答案
你需要告诉 spy 做一些事情:
spyOn(rootScope, 'testFn').andCallThrough();
我在这里更新了 plnkr:http://plnkr.co/edit/t3ksMtKSI3CEkCtpZ8tI?p=preview
希望这对您有所帮助!
关于javascript - Jasmine spyOn 范围函数中断测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21615847/