在“它”之外运行代码。阻止了我的Jasmine测试 [英] Running code outside the "it" block breaks my Jasmine test
问题描述
我正在尝试使用 jasmine编写一些测试用例(第一次)
describe("Widget App core logic", function () {
WAPP.widgets = [];
addwidget将在我的WAPP.widgets阵列
addwidget will add a widget in my WAPP.widgets array
WAPP.addWidget('testRecord', 'testRecordContent');
it("added", function () {
expect(WAPP.widgets.length).toEqual(1);
});
删除小部件将删除相同的小部件
Remove widget will remove same widget
WAPP.removeWidget('1');
it("record removed correctly", function () {
expect(WAPP.widgets.length).toEqual(0);
})
写完第二个规范后,我的第一个规范失败,因为它显示 WAPP .widgets为空。甚至虽然在第一个规范时有一个值 WAPP .widgets
After writing second spec my first spec fails as it shows WAPP .widgets is empty. even though at the time of first spec there is a value in WAPP.widgets
推荐答案
这里的问题是你不应该的测试代码在之外
。在执行所有测试用例之前, it
之外的代码运行一次。在您的情况下可能发生的是您在测试开始之前删除所有小部件。
The problem here is that you shouldn't have test code outside of it
. The code outside of the it
is ran once before the execution of all the test case. What is probably happening in your case is that you delete all the widget before the test even starts.
您的测试代码应该是这样的:
What your test code should look like is this :
describe("Widget App core logic", function () {
beforeEach(function () {
WAPP.widgets = [];
});
it("added", function () {
WAPP.addWidget('testRecord', 'testRecordContent');
expect(WAPP.widgets.length).toEqual(1);
});
it("record removed correctly", function () {
WAPP.addWidget('1', '1');
WAPP.removeWidget('1');
expect(WAPP.widgets.length).toEqual(0);
})
});
请注意您的测试代码应该是自包含的,所有初始化都应该在 it
或者 beforeEach
。
Do note that your test code should be self-contained, all the initialization should be done inside the it
or with beforeEach
.
这篇关于在“它”之外运行代码。阻止了我的Jasmine测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!