查看Angular in Action的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
执行之后<{1}}未定义,运行设置console.log
的{{1}}?
最后,自beforeEach
调用modelService
后,it(...)
函数的modelService
Object
值是否为karma
,然后运行beforeEach
?< / p>
答案 0 :(得分:1)
呃,是的,是的。
如果你想要更多细节,这里有一个最小的代码示例,演示了你要问的事情:
describe('suite', function() {
var foo;
beforeEach(function() {
foo = 'bar';
});
console.log('foo: ' + foo);
it('test', function() {
console.log('foo: ' + foo);
});
}
测试 runner (本例中为Karma)有两个不同的阶段:测试定义和测试执行。
在测试定义阶段,将执行测试脚本,这将导致构建包含测试和套件的内部数据结构。
describe
api;这个api将对套件名称进行一些记录,并将函数作为第二个参数调用。beforeEach
api;这个api会将作为参数传递的函数与直接封闭的测试套件相关联,但是不会执行它。console.log
语句,记录foo: undefined
it
api;这将记录测试名称和作为第二个参数传递的函数,将它们与直接封闭的测试套件相关联,但同样,传递的函数将不执行。现在,测试运行器将进入测试执行阶段,在此阶段,测试框架将遍历其内部数据结构并执行定义的测试。在这个例子中:
beforeEach
api的函数,将字符串值'bar'
指定给变量foo
。it
api的函数,导致foo: bar
被记录。这可能是你真正想知道的更多 - 希望你觉得它很有用。