2015-04-21 59 views
2

(這類似於這樣的問題:How to set up separate test and development database in meteor,但它是2歲的,從那時起流星有了很大的變化)流星 - 與測試數據庫中運行封裝測試

我試圖創建自己的包和我想運行單元測試。我想確保我的查詢是正確的,所以我想實際上對測試數據庫運行查詢,而不是僅僅存儲函數。

我有兩個問題:

  • 我怎麼能告訴流星反對測試數據庫運行我真實的,而不是?
  • 什麼是使用數據輕鬆填充測試數據庫的最佳方式?

理想情況下,我會設置一個步驟來清除比填充測試數據庫,所以我總是確切知道每個數據是什麼。

我是一個Tinytest新手(儘管我已經使用了其他單元測試框架),因此非常感謝代碼示例。

回答

3

這裏有類似於我們使用的一個例子:

var resetCollection = function(name) { 
    var Collection = this[name]; 
    if (Collection) 
    // if the collection is already defined, remove its documents 
    Collection.remove({}); 
    else 
    // define a new unmanaged collection 
    this[name] = new Mongo.Collection(null); 
}; 

reset = function() { 
    var collections = ['Comments', 'Posts']; 

    // reset all of the collections 
    _.each(collections(function(name) {resetCollection(name);})); 

    // insert some documents 
    var postId = Posts.insert({title: 'example post'}); 
    Comments.insert({postId: postId, message: 'example comment'}); 
}; 

Tinytest.add('something', function(test) { 
    reset(); 

    var post = Posts.findOne(); 
    var comment = Comments.findOne(); 
    return test.equal(comment.postId, post._id); 
}); 

在每次測試開始時我們稱之爲reset其清理數據庫,並創建必要的集合。

我該如何告訴Meteor針對測試數據庫而不是我的真實測試數據庫運行?

當您測試軟件包時,將爲您創建單獨的數據庫。沒有必要手動指定你的數據庫路徑。

使用數據輕鬆填充測試數據庫的最佳方法是什麼?

上面的例子應該給你一些指示。我發現避免軟件包間衝突的最好方法是在測試中使用非託管集合(名稱= null)。 resetCollection函數應該正確避免重新定義由其他包導出的任何託管集合。有關更多詳細信息,請參閱this question

+0

這看起來不錯。這對於自動分離數據庫也很棒。我在文檔中沒有看到任何有關這方面的信息。謝謝。 – samanime

+0

但這並不適用於客戶端。我收到一個錯誤,說我只能通過ID刪除。 –