Javascript Jasmine检查方法中是否定义了变量
我是茉莉花的新手。我的公司正在使用jasmine 1.3.1 我试图测试一个方法中是否定义了一个变量。我总是带着失败回来Javascript Jasmine检查方法中是否定义了变量,javascript,jasmine,Javascript,Jasmine,我是茉莉花的新手。我的公司正在使用jasmine 1.3.1 我试图测试一个方法中是否定义了一个变量。我总是带着失败回来 objectParentName.accessoriesSlider = { modifyJSON: function(obj) { var contentLoadJSON = []; for (var property in obj) { objectParentName.accessoriesSlider.cont
objectParentName.accessoriesSlider = {
modifyJSON: function(obj) {
var contentLoadJSON = [];
for (var property in obj) {
objectParentName.accessoriesSlider.contentJSONTotal++
contentLoadJSON.push(obj[property]);
}
contentLoadJSON = objectParentName.accessoriesSlider.sortJSON(contentLoadJSON, 'dateAdded', false);
return contentLoadJSON;
}
}
这是茉莉花的那个方法
describe('Test "modifyJSON" function', function () {
beforeEach(function() {
spyOn(objectParentName.accessoriesSlider, 'modifyJSON');
var contentLoadJSON = []; //forcibly add variable to see if that fixes issue
objectParentName.accessoriesSlider.modifyJSON();
});
it('is defined as "modifyJSON"', function () {
/**
* objectParentName.accessoriesSlider.modifyJSON should be defined.
*/
expect(objectParentName.accessoriesSlider.modifyJSON).toBeDefined();
});
it('and "modifyJSON" is a function', function () {
/**
* This should be a 'function'.
*/
expect(objectParentName.accessoriesSlider.modifyJSON).toBeFunction();
});
describe('Test "contentLoadJSON" variable', function () {
it('is defined', function () {
expect(contentLoadJSON).toBeDefined();
});
});
});
我得到了这个错误
ReferenceError: contentLoadJSON is not defined
at .<anonymous> (http://localhost:8234/spec/jasmine_accessories_slider.js:300:24)
at jasmine.Block.execute (http://localhost:8234/?spec=accessories_slider.js:1164:19)
at jasmine.Queue.next_ (http://localhost:8234/?spec=accessories_slider.js:2196:33)
at jasmine.Queue.start (http://localhost:8234/?spec=accessories_slider.js:2149:10)
at jasmine.Spec.execute (http://localhost:8234/?spec=accessories_slider.js:2476:16)
at jasmine.Queue.next_ (http://localhost:8234/?spec=accessories_slider.js:2196:33)
at jasmine.Queue.start (http://localhost:8234/?spec=accessories_slider.js:2149:10)
at jasmine.Suite.execute (http://localhost:8234/?spec=accessories_slider.js:2621:16)
at jasmine.Queue.next_ (http://localhost:8234/?spec=accessories_slider.js:2196:33)
at jasmine.Queue.start (http://localhost:8234/?spec=accessories_slider.js:2149:10)
ReferenceError:未定义contentLoadJSON
在(http://localhost:8234/spec/jasmine_accessories_slider.js:300:24)
在jasmine.Block.execute(http://localhost:8234/?spec=accessories_slider.js:1164:19)
在jasmine.Queue.next\u(http://localhost:8234/?spec=accessories_slider.js:2196:33)
在jasmine.Queue.start(http://localhost:8234/?spec=accessories_slider.js:2149:10)
在jasmine.Spec.execute(http://localhost:8234/?spec=accessories_slider.js:2476:16)
在jasmine.Queue.next\u(http://localhost:8234/?spec=accessories_slider.js:2196:33)
在jasmine.Queue.start(http://localhost:8234/?spec=accessories_slider.js:2149:10)
在jasmine.Suite.execute(http://localhost:8234/?spec=accessories_slider.js:2621:16)
在jasmine.Queue.next\u(http://localhost:8234/?spec=accessories_slider.js:2196:33)
在jasmine.Queue.start(http://localhost:8234/?spec=accessories_slider.js:2149:10)
因此,我不知道为什么这里会出现错误。第一个问题是变量contentLoadJSON
未在调用它的函数范围内定义
使用var
定义变量时,它将成为局部变量,并且只能在该函数的作用域内访问。在这种情况下,其存在的功能是:
beforeEach(function() {
spyOn(objectParentName.accessoriesSlider, 'modifyJSON');
var contentLoadJSON = [];
objectParentName.accessoriesSlider.modifyJSON();
});
获取错误的函数是一个不同的函数(){…}
,它没有嵌套在每个之前的中。因此,它无法看到您定义的局部变量
纠正这种情况的一种方法是在测试套件的范围内定义变量,而不是在每个
函数之前的范围内:
describe('Test "modifyJSON" function', function () {
var contentLoadJSON; // Define it here
beforeEach(function() {
contentLoadJSON = []; // Assign it here
// ... more code here ...
}
// ... more code here ...
describe('Test "contentLoadJSON" variable', function () {
it('is defined', function () {
// Test contentLoadJSON here...
第二个问题是,仅仅因为变量具有相同的名称并不意味着它们实际上是相同的变量。它们也必须在相同的范围内才能成为现实var contentLoadJSON
insidemodifyJSON
实际上是一个与您在测试套件中的var contentLoadJSON
之前定义的完全不同的变量
当您调用被测函数时,您并没有将其结果赋给要测试的变量。事实上,您实际上是在调用函数后立即丢弃结果
要解决此问题,请更改:
var contentLoadJSON = [];
objectParentName.accessoriesSlider.modifyJSON();
// Note that contentLoadJSON still equals [] at this point.
// The return of the function call is getting thrown away,
// because you don't assign it to a variable
致:
第三个问题是expect(someVariable).toBeDefined()代码>可能不会执行您期望它执行的操作。它不会检查变量是否“在范围内”。它检查值someVariable
是否不等于值undefined
。除非您的函数有时可能返回未定义的,,否则此特定测试并不重要,可能永远不会失败
最好检查结果是否为“truthy”(expect(contentLoadJSON).toBeTruthy()
)。甚至可以只检查它是否等于给定数据/输入的预期值代码>?
var contentLoadJSON = objectParentName.accessoriesSlider.modifyJSON();