Jasmine检查变量是否在方法中定义

时间:2016-06-22 14:28:15

标签: javascript jasmine

我是茉莉花的新手。我公司正在使用茉莉花1.3.1

我正在尝试测试变量是否在方法中定义。我不断回来失败。

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)

所以,我不知道为什么我在这里收到错误。

1 个答案:

答案 0 :(得分:2)

第一个问题是变量contentLoadJSON未在您调用它的函数范围内定义。

使用var定义变量时,它将成为局部变量,并且只能在该函数的范围内访问。在这种情况下,它所存在的功能是:

beforeEach(function() {
    spyOn(objectParentName.accessoriesSlider, 'modifyJSON');

    var contentLoadJSON = [];

    objectParentName.accessoriesSlider.modifyJSON();
});

您收到错误的函数是function() { ... },它没有嵌套在beforeEach中。因此无法看到您定义的局部变量。

解决此问题的一种方法是在测试套件的范围内定义变量,而不是在beforeEach函数的范围内:

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内的modifyJSON实际上是与您在测试套件var contentLoadJSON中定义的beforeEach完全不同的变量。

当您调用被测功能时,您不会将其结果分配给您要测试的变量。实际上,在调用函数后,您实际上是在丢弃结果。

要解决此问题,请更改:

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

要:

var contentLoadJSON = objectParentName.accessoriesSlider.modifyJSON();

第三个​​问题expect(someVariable).toBeDefined();可能无法完成您期望它做的事情。它不会检查变量是否在“范围内”。它会检查值someVariable是否不等于值undefined。除非你的函数有时会返回undefined,否则这个特定的测试不是很重要,可能永远不会失败。

最好检查结果是否为“truthy”(expect(contentLoadJSON).toBeTruthy())。甚至可以检查它是否等于您对给定数据/输入所期望的值。

相关问题