【发布时间】:2021-03-02 09:45:12
【问题描述】:
如上图所示,在第一个 it 块中分配的控制器值在第二个 it 块中不再相同。这是默认行为吗?我们可以在 karma 配置文件中做一些更改来解决这个问题吗?
本项目基于 angularjs 1.7.9,使用 karma 和 jasmine。
【问题讨论】:
标签: javascript angularjs unit-testing jasmine karma-jasmine
如上图所示,在第一个 it 块中分配的控制器值在第二个 it 块中不再相同。这是默认行为吗?我们可以在 karma 配置文件中做一些更改来解决这个问题吗?
本项目基于 angularjs 1.7.9,使用 karma 和 jasmine。
【问题讨论】:
标签: javascript angularjs unit-testing jasmine karma-jasmine
我认为你可以利用beforeEach。
describe('Login', function () {
beforeEach(() => {
$controller.name = 'Pankaj';
});
it('test one', function () {
console.log($controller.name);
});
it('test two', function () {
console.log($controller.name);
});
});
beforeEach 在每个 it 块之前运行。
编辑
您必须正确使用describe, beforeEach, beforeAll, afterEach, and afterAll。 describe 您可以按功能/情况进行拆分,beforeEach 在每个 it 之前运行,beforeAll 在 describe 块中运行一次,afterEach 在 describe 块中的每个 it 之后运行,以及afterAll 在所有 it 块在 desribe 块中完成后运行一次。
describe('Login', function () {
beforeEach(() => {
$controller.name = 'Pankaj';
});
it('test one', function () {
console.log($controller.name);
});
it('test two', function () {
console.log($controller.name);
});
describe('dynamic part', function () {
beforeEach(() => {
// call the function that will change the variable
});
it('test 1', function () {
// your assertions
});
it('test 2', function () {
// your assertions
});
});
});
【讨论】: