私はrendr(クライアントとサーバーのバックボーン)を使用してWebアプリケーションを作成し、単体テストを実装しようとしています。
モカでコレクションフェッチユニットテストを実行する方法はありますか? mocha で ajax リクエストをモックするために sinon-chai を使用したいと考えています。しかし、この方法で $ "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();
});
});
私の質問は、モカで単体テストを実行するときに $.ajax 関数をスタブする方法はありますか?