jasmine typeError不是函数

时间:2015-09-23 23:43:44

标签: javascript testing jasmine

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

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

while

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

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)

我明白了:

jasmine-node UserSpec.js

好吧,我的所有测试都已执行,而且它们都失败了,确定它是正确的TDD圈,但所有这些测试都应该通过。我无法理解为什么我会遇到这个多重错误:

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

我的两个脚本是真正导入的(用户创建得很好,而且真正调用了encrypt()方法),所以我无法弄清楚出了什么问题

有人知道这个bug的来源吗?

1 个答案:

答案 0 :(得分:3)

默认情况下,运行所有规范文件,因此测试运行第二个文件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声明来解决我的所有错误......