【问题标题】:How to jasmine test jQuery post ajax properties (e.g url)如何 jasmine 测试 jQuery post ajax 属性(例如 url)
【发布时间】:2014-03-04 01:47:19
【问题描述】:

我有这个基本的 jQuery 帖子到我想测试它的 url。我已经做了Sinon创建服务器测试,但是我也想测试方法中给出的url和属性,但是我得到了错误,因为jQuery.post(url, data).always(callback)不能在未定义的对象上调用。

我还应该测试什么?

这是我的 jQuery 代码。

define(['jquery'], function (jQuery) {
    var ProductListingService = function () {
        var self = this;

        self.create = function (sku, quantity, price, callback) {
            var url = '/url';
            var data = { 'sku': sku, 'quantity': quantity, 'price': price };

            jQuery.post(url, data).always(callback);

        };
    };

    return ProductListingService;
});

这是我的测试

define(['service/ProductListingService'], function (ProductListingService) {
    describe('ProductListingService', function () {

        var productListingService, server;

        beforeEach(function () {
            productListingService = new ProductListingService();
            server = sinon.fakeServer.create();

        });

        it('posts and call to /url', function () {
            server.respondWith('POST', '/url',
                                [201, { "Content-Type": "application/json" }, '']
            );

            var callback = sinon.spy();

            productListingService.create('sku', '1', 10.0, callback);
            server.respond();

            expect(callback.calledOnce).toBeTruthy();
        });

        it('posts and call to /url', function () {
            spyOn(jQuery, "post").andCallFake();

            productListingService.create('sku', '1', 10.0, sinon.spy());

            expect(jQuery.post.mostRecentCall.args[0]["url"]).toEqual("/api/listing/create");
        });

    });
});

【问题讨论】:

    标签: javascript jquery ajax jasmine sinon


    【解决方案1】:

    我认为更简单的解决方案是用 Jasmine 的 ajax.js (http://jasmine.github.io/2.0/ajax.html) 替换 sinon:

    define(['service/ProductListingService'], function (ProductListingService) {
        describe('ProductListingService', function () {
    
            var productListingService;
    
            beforeEach(function () {
                productListingService = new ProductListingService();
                jasmine.Ajax.install();
            });
    
            afterEach(function() {
                jasmine.Ajax.uninstall();
            });
    
            it('posts and call to /url', function () {
                var callback = sinon.spy();
    
                productListingService.create('sku', '1', 10.0, callback);
    
                var request = jasmine.Ajax.requests.mostRecent();
                request.response({
                    "status": 201,
                    "contentType": "application/json",
                    "responseText": ""
                });
    
                expect(request.method).toEqual("POST");
                expect(request.url).toEqual("/url");
                expect(callback.calledOnce).toBeTruthy();
            });
    
            it('posts and call to /url', function () {
                productListingService.create('sku', '1', 10.0, sinon.spy());
    
                expect(jasmine.Ajax.requests.mostRecent().url).toEqual("/api/listing/create");
            });
    
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多