【问题标题】:TypeScript, Mongoose, Jasmine - requiring schemasTypeScript、Mongoose、Jasmine - 需要模式
【发布时间】:2017-02-21 02:34:01
【问题描述】:

我已经为如何构建合适的测试工作流程而苦苦挣扎了好几天。我意识到测试应该模拟数据库依赖关系,但情况是我需要测试整个过程以及真实的数据库查询。 在下面的代码中,我需要我的模型,以便我可以对 db 执行操作,例如删除测试数据和推送测试数据。

问题是:

  1. 引用架构/模型的正确方法是什么?

  2. 或者有没有使用 typescript 编写 Jasmine 测试的方法?

代码不起作用,因为它说 BucketConfigS.remove 不是函数:

'use strict';
let BucketConfigS = require('../dist/app/BucketConfig/BucketConfigSchema');

describe('Bucket config collection:', () => {
    describe('GetAll service -', () => {
        describe('Given that there are no configs', function () {
            beforeEach(done => {
                done();
            });
            afterEach(done => {
                BucketConfigS.remove({}, done);
                done();
            });
            it('should return an empty array', function () {
               // test case
            });
        });
    });
});

我还尝试了以下要求行:

let BucketConfigS = require('../dist/app/BucketConfig/BucketConfigSchema').default;

但是它破坏了整个测试套件(没有写出测试结果)。

架构文件如下所示:

"use strict";
var DataAccess_1 = require("./../common/DataAccess");
var mongoose = DataAccess_1.DataAccess.mongooseInstance;
var mongooseConnection = DataAccess_1.DataAccess.mongooseConnection;
var BucketConfigSchema = (function () {
    function BucketConfigSchema() {
    }
    Object.defineProperty(BucketConfigSchema, "schema", {
        get: function () {
            var schema = mongoose.Schema({
                AppName: {
                    type: String,
                    required: true
                },
                Platform: {
                    type: String,
                    required: true
                },
                Segment: {
                    type: String,
                    required: true
                },
                UpdateTime: {
                    type: Date,
                    default: Date.now
                }
            });
            return schema;
        },
        enumerable: true,
        configurable: true
    });
    return BucketConfigSchema;
}());
var BucketConfig = mongooseConnection.model("BucketConfig", BucketConfigSchema.schema);
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = BucketConfig;

这是一个编译结果:

import { DataAccess } from "./../common/DataAccess";
import { IBucketConfig } from "./IBucketConfig";

let mongoose = DataAccess.mongooseInstance;
let mongooseConnection = DataAccess.mongooseConnection;

class BucketConfigSchema {

    static get schema() {
        let schema = mongoose.Schema({
            AppName: {
                type: String,
                required: true
            },
            Platform: {
                type: String,
                required: true
            },
            Segment: {
                type: String,
                required: true
            },
            UpdateTime: {
                type: Date,
                default: Date.now
            }
        });

        return schema;
    }
}

let BucketConfig = mongooseConnection.model<IBucketConfig>("BucketConfig", BucketConfigSchema.schema);
export default BucketConfig;

【问题讨论】:

    标签: node.js mongodb typescript mongoose jasmine


    【解决方案1】:

    不知道为什么,但是

    let BucketConfigS = require('../dist/app/BucketConfig/BucketConfigSchema').default;
    

    开始工作了……

    【讨论】:

      猜你喜欢
      • 2021-08-20
      • 2020-04-24
      • 2015-01-31
      • 2020-05-29
      • 2012-12-07
      • 2019-01-13
      • 2016-01-12
      • 2021-10-18
      • 1970-01-01
      相关资源
      最近更新 更多