茉莉花类型错误不是函数

jasmine typeError is not a function

我要构建一个 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 

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

有人知道错误的来源吗?

默认情况下,all spec-files 是 运行,所以第二个文件 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 语句解决了我所有的错误...