【发布时间】:2015-07-05 23:11:25
【问题描述】:
我正在为 AngularJS 开发一些 TDD(完全是另一个故事)并且遇到了我的 beforeEach 调用显然没有被执行的情况。我将其简化为以下示例。
console.log 消息在 beforeEach 中的事实证明了这一点,并且它都出现了:
describe('userApp', function(){
beforeEach( function(){
console.log("in beforeEach...");
});
it('should be able to log something', function(){
console.log("in it...");
});
});
这不起作用,因为 beforeEach 中的 console.log 消息没有显示,并且在尝试 $log.info 时失败并抛出错误消息:TypeError: Cannot read property 'info' of undefined
describe('userApp', function(){
var $log;
beforeEach(module('userApp', function($provide) {
console.log("in beforeEach...");
// Output messages
$provide.value('$log', console);
}));
it('should be able to log something', function(){
console.log("in it...");
$log.info("Using $log for logging...");
});
});
我正在使用 Angular 1.3.15、karma 0.12.31、jasmine 2.3.4。可能我忽略了一些明显的东西......
编辑:Michael Radionov 的解释很有帮助;但是,我不明白为什么这个修改后的代码仍然会抛出同样的错误。
describe('userApp', function(){
console.log("starting TEST3"); <=== this prints
var $log;
beforeEach(function() {
console.log("TEST3: in beforeEach..."); <=== this prints
module('userApp', function($provide, _$log_) {
$log = _$log_;
console.log("TEST3: in beforeEach/module..."); <=== never executed
// Output messages
$provide.value('$log', console);
$log.info("TEST3: calling $log in beforeEach...");
})
});
it('should be able to log something', function(){
console.log("TEST3: in it...");
$log.info("TEST3: Using $log for logging..."); <=== $log undefined err
});
});
此外,“module('userApp'...”中的代码似乎从未执行过...?
【问题讨论】:
标签: angularjs tdd karma-jasmine