mercredi 23 septembre 2015

jasmine typeError is not a function

I'm gonna build a node.js app an I was looking for unit testing on JS. So I tried Jasmine, seems to be fun and cool at use.

Well I've a spec File : 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)

when I run it by console with cmd by :

jasmine-node UserSpec.js

I got this :

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

Well , all my tests are executed, and they all fail, ok it's correct TDD circle, but all theses tests should pass. I can't get why I've got this multiple error :

 TypeError: undefined is not a function 

My two scripts are really imported (the user is well created , and the encrypt() method is really called), so i can't figure what's going wrong

Does someone have an idea of the source of the bug ?
Thx in advance
@Azrael_404

SOLUTION : By default, ALL spec-files are run, so a second file "SpecHelper.js" is ran by the test. It's a default file in demo package so i don't checked it. The fact is this file contain this :

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

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

A beforeEach event'll be called before all test, that's why i got error on ALL my tests !

Aucun commentaire:

Enregistrer un commentaire