[英]Mocha run rendr collection ajax unit test
我嘗試使用rendr(客戶端和服務器中的主干)創建一個webapp,並嘗試實施一些單元測試。
有沒有一種方法可以在Mocha中運行集合獲取單元測試? 我想使用sinon-chai在摩卡中模擬ajax請求。 但是當我嘗試以這種方式存根$“ ajax”時,就會發生錯誤,
var chai = require("chai");
var rekuire = require("rekuire");
var sinon = require("sinon");
var $ = require("jquery"); //because this is node-jquery, we don't have ajax function.
chai.use(require("sinon-chai"));
require("chai").should();
require("mocha-sinon");
var Books = rekuire("app/collections/books");
describe("Books interaction with REST API", function() {
it("should load using the API", function(){
//TypeError: Cannot stub non-existent own property ajax
this.ajax_stub = this.sinon.stub($, "ajax").yieldsTo("success", [
{
count: 20,
"books": [
{
title: "Google1",
author: ["author1", "author2"]
},
{
title: "Google2",
author: ["author3", "author4"]
}
]
}
]);
this.books = Books.fetch();
this.books.should.have.length(2);
this.ajax_stub.restore();
});
});
我的問題是,當我們在Mocha中運行單元測試時,是否有辦法存根$ .ajax函數?
對於rendr:
測試存在一些問題。 模型/集合獲取實際上使用async.parallel,而不是$ .ajax。
另外,除非覆蓋它,否則我不建議您專門測試.fetch
。 此功能已經在Rendr本身內部進行過測試,因此該測試不會給您帶來太多實用性。 如果您要覆蓋,我建議您僅對collection.fetch
函數進行存根處理,並使用該結果代替$
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.