简体   繁体   English

Mocha Run Rendr Collection Ajax单元测试

[英]Mocha run rendr collection ajax unit test

I try to use rendr(backbone in client&server) to create a webapp and try to implemented some unit test. 我尝试使用rendr(客户端和服务器中的主干)创建一个webapp,并尝试实施一些单元测试。

Is there a way to run collection fetch unit test in mocha? 有没有一种方法可以在Mocha中运行集合获取单元测试? I want to use sinon-chai to mock ajax request in mocha. 我想使用sinon-chai在摩卡中模拟ajax请求。 But Error happen when I try to stub $ "ajax" in this way, 但是当我尝试以这种方式存根$“ 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();
    });
}); 

My question is, is there a way to stub $.ajax function when we run unit test in mocha? 我的问题是,当我们在Mocha中运行单元测试时,是否有办法存根$ .ajax函数?

For rendr: 对于rendr:

There are a few issues with the test. 测试存在一些问题。 Model / collection fetch actually uses async.parallel, not $.ajax. 模型/集合获取实际上使用async.parallel,而不是$ .ajax。

Also, I wouldn't recommending specifically testing .fetch unless you're overriding it. 另外,除非覆盖它,否则我不建议您专门测试.fetch This function is already tested inside of Rendr itself, so the test won't give you much utility. 此功能已经在Rendr本身内部进行过测试,因此该测试不会给您带来太多实用性。 If you're overriding, I would suggest just stubbing out the collection.fetch function, and have that yield instead of $ . 如果您要覆盖,我建议您仅对collection.fetch函数进行存根处理,并使用该结果代替$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM