繁体   English   中英

Sinon FakeServer在Mocha中不起作用

[英]Sinon FakeServer not working in Mocha

我正在尝试为API调用设置测试。 我在before方法中创建伪造的服务器,并测试基本实现,我在使用$.ajax与实际的api调用。 但是,在任何时候我都不会在server.requests看到任何请求。 我的ajax调用使用cannot call method open of undefined方法触发错误方法。 我正在导入Sinon,sinonFakeHttps和sinonFakeServer。 我想念什么。 在论坛上度过了2天没有运气

这是我的代码。

    describe('Warehouse Row', function (){
        before(function(){
            server = sinon.fakeServer.create();
            server.autoRespond = true;

        });
        after(function(){
            server.restore();
        });

        beforeEach(function(){
            sut = new Sut();
            sut.start();

        });
        it('should exist', function(){
            should.exist(sut);
        });

        it('setting value to positive int should validate',function(done){

            server.respondWith(200, { 'Content-Type': 'application/json' },'{ "stuff": "is", "awesome": "in here" }');


            var callback = sinon.spy();

            $.ajax({
                url: '/something',
                success: function(){
                    callback();
                    callback.should.have.been.called;
                    done();
                },
                error : function(err){
                    console.log(err);
                }
            });
      });

我至少看到您的代码存在一个问题,即您没有使用数组调用server.respondWith 尝试用以下命令替换该行:

server.respondWith([200, { 'Content-Type': 'application/json' },'{ "stuff": "is", "awesome": "in here" }']);

我创建了一个似乎有效的小提琴

我有同样的问题。 我在完全按照应有的方式设置了Sinon假服务器(使用Mocha和Chai,测试了Backbone应用),但出现了错误:

statusText: "TypeError: Cannot call method 'open' of undefined"

问题是我使用的是Bower Sinon发行版,出于某种原因,该发行版似乎无法正常运行(其中一个原因,必须单独需要fakeServer ,并且不会自动加载其自身的依赖项)。

我切换到了看起来更完整的CDN版本(位于http://cdnjs.cloudflare.com/ajax/libs/sinon.js/1.7.3/sinon-min.js ),并修复了该问题。

供参考,测试代码:

require([
    '../js/vendor/bower/chai/chai',
    '../js/vendor/bower/mocha/mocha',

    // Originally was:
    // '../js/vendor/bower/sinon/lib/sinon',
    // '../js/vendor/bower/sinon/lib/sinon/util/fake_xml_http_request',
    // '../js/vendor/bower/sinon/lib/sinon/util/fake_server',

    // Changed to:
    'http://cdnjs.cloudflare.com/ajax/libs/sinon.js/1.7.3/sinon-min.js'
],
function(chai) {
    mocha.setup('bdd');

    var expect = chai.expect;
    mocha.setup({
        ui: 'bdd',
        bail: false
    });

    require(['app'],
    function(App) {
        describe('App.Models.Question', function() {
            var server, question;

            beforeEach(function() {
                question = new App.Models.Question();

                server = sinon.fakeServer.create();
                server.autoRespond = true;
            });

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

            it('can be saved', function(done) {
                server.respondWith([200, { 'Content-Type': 'application/json' },'{"status":"200"}']);

                var cb = function(success) {
                        expect(success).to.be.ok;
                        done();
                    }
                question.save(null, {
                    success: function(model, resp) {
                        cb(true);
                    },
                    error: function(model, resp) {
                        cb(false);
                    }
                });
            });
        });
    });
});

暂无
暂无

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

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