【问题标题】:Uncaught TypeError: mongoose.model is not a function - Jasmine未捕获的类型错误:mongoose.model 不是函数 - Jasmine
【发布时间】:2017-01-24 22:10:20
【问题描述】:

无法在 node.js 上使用 jasmine-karma 运行测试。

我已经安装了 karma、jasmine、browserify、node 和 mongoose,我尝试运行我的测试但我收到错误“Uncaught TypeError: mongoose.model is not a function” 和“... 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;

【问题讨论】:

    标签: node.js jasmine browserify karma-jasmine


    【解决方案1】:

    您正在使用带有三个参数的 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;
    

    【讨论】:

    • 您的更改没有帮助,仍然是同样的问题。模型函数中的大约三个 args 我从这里得到这个提示 stackoverflow.com/a/7722490/2510058 并且它对我的工作正常。所以这不是 100% 的原因
    • 你的猫鼬版本是什么?
    • 我的猫鼬版本是3.10.5
    • @user2510058 你最后做了什么?我有同样的问题。 stackoverflow.com/questions/41004659/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 2022-11-19
    • 1970-01-01
    • 2019-06-06
    • 2019-05-24
    • 2021-12-15
    • 2019-10-26
    相关资源
    最近更新 更多