未捕获的类型错误:mongoose.模型不是函数- Jasmine



无法运行test,在node.js上使用jasmine-karma

我已经安装了,karma, jasmine, browserify,node和mongoose,我试图运行我的测试,但我得到错误"未捕获的类型错误:mongoose。模型不是函数"one_answers"…user.js: 10:0"。

karma.conf.js

// Karma configuration
// Generated on Fri Sep 16 2016 15:23:39 GMT+0200 (Środkowoeuropejski czas letni)
module.exports = function(config) {
  config.set({
    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',

    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['browserify','jasmine'],

    // list of files / patterns to load in the browser
    files: [
      'test-main.js',
      'spec/server/**/*.js',
      'server/scripts/auth.js'
    ],
    browserify: {
      debug: true,
      transform: [ 'brfs' ]
    },
    // list of files to exclude
    exclude: [
    ],

    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
      'server/scripts/auth.js': [ 'browserify' ]
    },

    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress', 'jasmine-spec-runner'],
    jasmineSpecRunnerReporter: {
        jasmineCoreDir: 'jasmine-core'
    },

    // web server port
    port: 9876,

    // enable / disable colors in the output (reporters and logs)
    colors: true,

    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,

    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,

    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],

    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,
    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity}) }

authSpec.js

describe("Test auth", function () {
    describe("passwordVerification", function () {
        var Auth;
        beforeEach(function () {
            Auth = new auth();
        });
        it("is password same", function () {
            expect(Auth.isAlphaNumeric("asd111")).toBeTruthy();
        });
    });
});

auth.js

"use strict";
var mongoose = require('mongoose');
var session = require('express-session');
var User = require('../models/user.js');
var Auth = {
//...
    isAlphaNumeric(val) {
        if (val.match(/^[a-zA-Z0-9]+$/)) {
            return true;
        }
        else {
            return false;
        }
    }
}
module.exports = Auth;

user.js

'use strict';
var mongoose = require('mongoose');
var UserSchema = mongoose.Schema({
    email              : String,
    password           : String,
    isFacebookAccount  : Boolean
});
UserModel = mongoose.model("Users", UserSchema, "users");
module.exports = UserModel;

您正在使用带有三个参数的model函数,这不是一个有效的声明。

试着写这个

user.js

'use strict';
var mongoose = require('mongoose');
var UserSchema = mongoose.Schema({
    email              : String,
    password           : String,
    isFacebookAccount  : Boolean
});
UserModel = mongoose.model("Users", UserSchema);
module.exports = UserModel;

你只需要在运行程序时连接数据库,那么这个错误就不会发生

最新更新