我尝试使用 rendr(backbone in client&server) 创建一个 webapp 并尝试实现一些单元测试。
有没有办法在 mocha 中运行集合获取单元测试?我想用 sinon-chai 在 mocha 中模拟 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 函数?