Sinon的假服务器没有响应 [英] Sinon's fake server is not responding

查看:122
本文介绍了Sinon的假服务器没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还有很多其他问题询问Sinon没有回复,但他们似乎都解决了一些平凡的问题,如无效的响应数据或切换的配置选项。

There are a number of other questions asking about Sinon failing to respond, but they all seem to resolve with something mundane, like invalid response data or a toggled configuration option.

我的情况如下:

在主应用程序中( / js / app / )requireJS是用于加载网站应用程序模块。

对于测试(在 / js / test ),requireJS也用于加载相同的模块,但添加摩卡 Sinon

In the main application (at /js/app/) requireJS is used to load website application modules.
For testing (at /js/test) requireJS is also used to load the same modules, but adds Mocha, Chai, and Sinon.

这是自助的测试应用程序:

define(
    "testRunner",
    ["require", "chai", "module", "sinon", "mocha"],
    function( require, chai, module ){
        // Chai setup
        assert = chai.assert;
        should = chai.should();
        expect = chai.expect;

        // Mocha setup
        mocha.setup( 'bdd' );

        // tests
        require(
            module.config().tests,
            function(){
                mocha.run();
            }
        );
    }
);

require(["testRunner"]);

module.config()。tests is在 requirejs.config({})中定义的调用:

module.config().tests is defined in the requirejs.config({}) call as:

"config": {
    "testRunner": {
        "tests": [
            "test/format",
            "test/transfer"
        ]
    }
}

当Mocha处理转移测试,Sinon失败,测试超时。

When Mocha processes the transfer tests, Sinon fails, and the test times out.

这是转移的全部内容测试:

define(
    ["transfer"],
    function( Transfer ){
        Transfer = new Transfer();

        describe( "Transfer", function(){
            describe.only( "#loadSomeData", function(){
                it( "should load the test data", function( done ){
                    var server = sinon.fakeServer.create();

                    server.autoRespond = true;
                    server.respondWith( "string" );

                    var async = Transfer.loadSomeData( 123 );

                    async.done( function( data, s, x ){
                        data.should.equal( "string" );
                        done();
                    });

                    server.respond();
                });
            });
        });
    }
);

testRunner输出变为:

超时2000ms超出

The testRunner output becomes:
timeout of 2000ms exceeded

对于它的价值, Transfer.loadSomeData()将返回 promise :jQuery的输出 $ .ajax()或立即解决的对新 $。延迟对象。

在任何一种情况下,响应都是一个Ajax包装器,它将使用响应数据解析。

For what it's worth, Transfer.loadSomeData() will return a promise: either the output of jQuery's $.ajax() or the immediately resolved promise to a new $.Deferred object.
In either case, the response is an Ajax wrapper that will resolve with the response data.

Sinon永远不会发出响应 - Ajax调用只是超时。

经过多次试验和错误后,我将其缩减为最简单的解决方案 - 仍然失败。

那个解决方案是 jsfiddle here

Sinon never emits the response - the Ajax call simply times out.
After much trial and error, I reduced it to the simplest possible solution - which still failed.
That solution is on jsfiddle here.

sinon 在这里发生了什么?

为什么响应永远不会被发送回调用位置(在这种情况下,jQuery的 #ajax )?

<$ c中是否有错误$ c> sinon ?

What is going on with sinon here?
Why is the response never being emitted back to the calling location (jQuery's #ajax, in this case)?
Is there a bug in sinon?

我做错了什么,如何修复它以使这个例子有效?

推荐答案

虽然问题实际上没有解决,但我通过对Sinon使用略有不同的语法解决了这个问题。

While the problem isn't actually solved, I have worked around this issue by using a slightly different syntax for Sinon.

这是我的测试套件:

describe( "Transfer", function(){
    var suite = this;

    beforeEach( function(){
        suite.server = sinon.fakeServer.create();
    });

    afterEach( function(){
        suite.server.restore();
    });

    describe( "#loadSomeData", function(){
        it( "should load the test data", function( done ){
            var async = Transfer.loadSomeData( 123 );

            suite.server.requests[0].respond(
                200,
                { "Content-Type": "application/json" },
                JSON.stringify({test: "success"})
            );

            async.done( function( data, s, x ){
                data.test.should.equal( "success" );
                done();
            });
        });
    });
});

正如您所看到的,我明确地选择了第一个请求对象,而不是简单地指示服务器做出响应( requests [0] ),然后调用它的响应方法。

As you can see, instead of simply instructing the server to respond, I explicitly select the first request object (requests[0]), then call the respond method on it.

我不知道以前的语法( server.respond())是否真的有效,但明确选择来自服务器已收到的请求列表中的请求对象并直接响应它可以正常工作。

I don't know if the previous syntax (server.respond()) actually works, but explicitly selecting the request object from the list of requests the server has received and responding to it directly works fine.

这篇关于Sinon的假服务器没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆