admin管理员组文章数量:1334159
Case
When I create a spy on rootScope
, the expectation fails for some reason. Check out the plunkr and try just menting 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);
});
});
Case
When I create a spy on rootScope
, the expectation fails for some reason. Check out the plunkr and try just menting 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);
});
});
Share
Improve this question
asked Feb 6, 2014 at 22:53
km6zlakm6zla
4,9072 gold badges31 silver badges53 bronze badges
1 Answer
Reset to default 10You 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!
本文标签: javascriptJasmine spyOn scope function breaks testStack Overflow
版权声明:本文标题:javascript - Jasmine spyOn scope function breaks test - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742365362a2461166.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论