gpt4 book ai didi

javascript - Jasmine 类型错误不是函数

转载 作者:搜寻专家 更新时间:2023-11-01 04:23:56 26 4
gpt4 key购买 nike

我要构建一个 node.js 应用程序,我正在寻找 JS 单元测试。所以我尝试了Jasmine,使用起来似乎很有趣也很酷。

我有一个规范文件:UserSpec.js

var userClass = require('../src/users.js');
var utils = require('../src/utils.js');
describe("Users tests", function() {

var usernameTest = "usernameTest";
var emailTest = "emailTest";
var passwordTest ="passwordTest";
var myUser = userClass.create('usernameTest','emailTest','passwordTest');

/**********
* TDD *
**********/
it("is not null", function() {
expect(myUser).not.toBe(null);
});
it("username not null", function() {
var username = myUser.username;
expect(username).not.toBe(null);
});
it("is a string", function() {
var usernameType = typeof myUser.username;

expect(usernameType).toEqual("string");
});
it("correct username", function() {
var username = myUser.username;
expect(username).toEqual(usernameTest);
});
it("mail not null", function() {
var email = myUser.email;
expect(email).not.toBe(null);
});
it("correct mail", function() {
var email = myUser.email;
expect(email).toEqual(emailTest);
});
it("password not null", function() {
var password = myUser.password;
expect(password).not.toBe(null);
});
it("password is encrypted", function() {
var password = myUser.password;
expect(password).toEqual(utils.encrypt(password));
});
//TODO: User BDD (CRUD)

当我使用 cmd 通过控制台运行它时:

jasmine-node UserSpec.js

我明白了:

D:\workspace\MyDL-NodeServer\spec>jasmine-node UserSpec.js
FFFFFFFFF

Failures:

1) is not null
Message:
TypeError: undefined is not a function
Stacktrace:
TypeError: undefined is not a function
at null.<anonymous> (D:\workspace\MyDL-NodeServer\spec\SpecHelper.js:2:11)
at Object.jasmine.executeSpecsInFolder (C:\Users\Azrael\AppData\Roaming\npm\node_modules\jasmine-node\lib\jasmine-node\index.js:168:16)
at Object.<anonymous> (C:\Users\Azrael\AppData\Roaming\npm\node_modules\jasmine-node\lib\jasmine-node\cli.js:248:9)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)

2) username not null
Message:
TypeError: undefined is not a function
Stacktrace:
TypeError: undefined is not a function
at null.<anonymous> (D:\workspace\MyDL-NodeServer\spec\SpecHelper.js:2:11)
at Timer.listOnTimeout (timers.js:119:15)

3) is a string
Message:
TypeError: undefined is not a function
Stacktrace:
TypeError: undefined is not a function
at null.<anonymous> (D:\workspace\MyDL-NodeServer\spec\SpecHelper.js:2:11)
at Timer.listOnTimeout (timers.js:119:15)

4) correct username
Message:
TypeError: undefined is not a function
Stacktrace:
TypeError: undefined is not a function
at null.<anonymous> (D:\workspace\MyDL-NodeServer\spec\SpecHelper.js:2:11)
at Timer.listOnTimeout (timers.js:119:15)

5) mail not null
Message:
TypeError: undefined is not a function
Stacktrace:
TypeError: undefined is not a function
at null.<anonymous> (D:\workspace\MyDL-NodeServer\spec\SpecHelper.js:2:11)
at Timer.listOnTimeout (timers.js:119:15)

6) correct mail
Message:
TypeError: undefined is not a function
Stacktrace:
TypeError: undefined is not a function
at null.<anonymous> (D:\workspace\MyDL-NodeServer\spec\SpecHelper.js:2:11)
at Timer.listOnTimeout (timers.js:119:15)

7) password not null
Message:
TypeError: undefined is not a function
Stacktrace:
TypeError: undefined is not a function
at null.<anonymous> (D:\workspace\MyDL-NodeServer\spec\SpecHelper.js:2:11)
at Timer.listOnTimeout (timers.js:119:15)

8) password is encrypted
Message:
TypeError: undefined is not a function
Stacktrace:
TypeError: undefined is not a function
at null.<anonymous> (D:\workspace\MyDL-NodeServer\spec\SpecHelper.js:2:11)
at Timer.listOnTimeout (timers.js:119:15)

9) password is encrypted
Message:
Expected 'c9a7773c3a130e3ca370dad6' to equal 'daff65787a4b4f3bc47498911683901d85c88294dd1495c5'.
Stacktrace:
Error: Expected 'c9a7773c3a130e3ca370dad6' to equal 'daff65787a4b4f3bc47498911683901d85c88294dd1495c5'.
at null.<anonymous> (D:\workspace\MyDL-NodeServer\spec\UserSpec.js:47:20)
at Timer.listOnTimeout (timers.js:119:15)

Finished in 0.037 seconds
8 tests, 18 assertions, 9 failures, 0 skipped

好吧,我的所有测试都已执行,但都失败了,好吧,这是正确的 TDD 循环,但所有这些测试都应该通过。我不明白为什么会出现这个多重错误:

 TypeError: undefined is not a function 

我的两个脚本真的是imported(用户创建的好,encrypt()方法真的调用了),所以我想不出哪里出了问题

有人知道错误的来源吗?

最佳答案

默认情况下,运行所有 规范文件,因此测试运行第二个文件SpecHelper.js。它是演示包中的默认文件,所以我没有检查它。事实上这个文件包含这个:

beforeEach(function () {
jasmine.addMatchers({
toBePlaying: function () {
return {
compare: function (actual, expected) {
var player = actual;

return {
pass: player.currentlyPlayingSong === expected && player.isPlaying
};
}
};
}
});
});

beforeEach 函数将在所有测试之前被调用,这就是我在所有测试中遇到错误的原因!

所以我通过注释 beforeEach 语句解决了我所有的错误...

关于javascript - Jasmine 类型错误不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32751209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com