当我在rootScope
上创建间谍时,由于某种原因,期望失败。检查一下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);
});
});
答案 0 :(得分:10)
你需要告诉间谍做点什么:
spyOn(rootScope, 'testFn').andCallThrough();
我在这里更新了plnkr:http://plnkr.co/edit/t3ksMtKSI3CEkCtpZ8tI?p=preview
希望这有帮助!