TypeScript,Mongoose,Jasmine - 需要模式

时间:2016-10-12 10:15:22

标签: node.js mongodb typescript mongoose jasmine

我一直在努力建立一个合适的测试工作流程。我意识到测试应该模拟数据库依赖,但情况是我需要测试整个过程,以及真正的数据库查询。 在下面的代码中,我需要我的模型,以便我可以在db上执行操作,例如删除测试数据和推送测试数据。

问题是:

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

  2. 或者有没有办法使用打字稿编写Jasmine测试?

  3. 代码不起作用,因为它说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;
    

1 个答案:

答案 0 :(得分:0)

Not sure why, but

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

started working...