【发布时间】: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