通过查看来自karma的角度在行动中测试,我运行了以下测试。
describe("Service: angelloModel", function() {
// load the service's module
beforeEach(module("Angello"));
var modelService;
//Initialize the service
beforeEach(inject(function (angelloModel) {
modelService = angelloModel;
console.log("modelService:", modelService);
}));
console.log("after| modelService:", modelService);
it("it should return seven different statuses", function() {
console.log("it: modelService", modelService);
...
});
});控制台记录如下:
after| modelService: undefined
modelService: Object ...
it: modelService Object ..自运行设置modelService的beforeEach之前执行console.log以来,modelService是否未定义?
最后,it(...)函数是否具有modelService的Object值,因为karma调用了beforeEach,然后运行it。
发布于 2014-01-04 01:37:16
呃,是的,是的。
如果您想要更多的细节,下面是一个最简单的代码示例,它演示了您要问的内容:
describe('suite', function() {
var foo;
beforeEach(function() {
foo = 'bar';
});
console.log('foo: ' + foo);
it('test', function() {
console.log('foo: ' + foo);
});
}测试运行程序(在本例中为Karma)有两个不同的阶段:测试定义和测试执行。
在测试定义阶段,将执行测试脚本,这将导致构建包含测试和套件的内部数据结构。
describe api;该api将记录套件名称,并调用作为其第二个参数传递的函数。beforeEach api;该api将把作为其参数传递的函数与立即封装的测试套件关联起来,但不会执行它。console.log语句,记录foo: undefinedit api;这将记录测试名称和作为其第二个参数传递的函数,并将它们与立即封装的测试套件相关联,但传递的函数将不会被执行。现在,测试运行程序将进入测试执行阶段,在此阶段,测试框架将遍历其内部数据结构并执行定义的测试。在本例中:
beforeEach api的函数将被执行,将字符串值'bar'分配给变量foo。it api的函数将被执行,导致foo: bar被记录下来。这可能是你真正想知道的更多--希望你发现它有用。
https://stackoverflow.com/questions/20907563
复制相似问题