我在用茉莉编写单元测试时遇到了一个问题,我设法将其简化为一个非常基本的测试方案:

describe('weird shit', function () {
    var myVal;
    beforeEach(myVal = 0);
    it('throws for some reason', function () {
        expect(myVal).toBe(0);
    });
});


并抛出TypeError:

Test 'weird shit:throws for some reason' failed
    TypeError: undefined is not a constructor (evaluating 'queueableFn.fn.call(self.userContext)') in

file:///C:/USERS/9200378/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/14.0/EXTENSIONS/SKJV5WFA.151/TestFiles/jasmine/v2/jasmine.js (line 1886)
    run@file:///C:/USERS/9200378/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/14.0/EXTENSIONS/SKJV5WFA.151/TestFiles/jasmine/v2/jasmine.js:1874:20
    execute@file:///C:/USERS/9200378/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/14.0/EXTENSIONS/SKJV5WFA.151/TestFiles/jasmine/v2/jasmine.js:1859:13
    queueRunnerFactory@file:///C:/USERS/9200378/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/14.0/EXTENSIONS/SKJV5WFA.151/TestFiles/jasmine/v2/jasmine.js:697:42
    execute@file:///C:/USERS/9200378/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/14.0/EXTENSIONS/SKJV5WFA.151/TestFiles/jasmine/v2/jasmine.js:359:28
    fn@file:///C:/USERS/9200378/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/14.0/EXTENSIONS/SKJV5WFA.151/TestFiles/jasmine/v2/jasmine.js:2479:44


如果我删除了beforeEach,那么它可以正常工作:

describe('weird shit', function () {
    var myVal =0;
    it('throws for some reason', function () {
        expect(myVal).toBe(0);
    });
});


我不明白,beforeEach是非常基本的,请帮忙。

最佳答案

beforeEach接受一个函数,您传递了myVal = 0的结果,它是0,这不是函数。

beforeEach(myVal = 0);


将上面的代码替换为以下代码:

beforeEach(function() {
  myVal = 0;
});


有关更多信息,请参考jasmine 2.5 here的文档。

关于javascript - TypeError:在 Jasmine 测试中使用beforeEach时,undefined不是构造函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40720976/

10-11 06:09