2016-10-12 53 views
0

我一直在爲如何構建正確的測試工作流而苦苦掙扎。我意識到測試應該模擬數據庫依賴性,但情況是我需要測試整個過程以及真正的數據庫查詢。 在下面的代碼中,我需要我的模型,以便我可以在db上執行操作,例如刪除測試數據和推送測試數據。TypeScript,Mongoose,Jasmine - 需要模式

的問題是:

  1. 什麼是引用模式/模式的正確方法是什麼?

  2. 或者有沒有使用打字機寫茉莉花測試的方法?

的代碼不能正常工作,因爲它說,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; 

但是它brokes整個測試套件(沒有測試結果寫出來)。

模式文件看起來是這樣的:

"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; 

回答

0

不知道爲什麼,但

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

開始工作......