如何测试ember-data已被提交给服务器?
问题描述:
我有一个模型,通过余烬数据休息适配器保存到我的服务器。如何测试ember-data已被提交给服务器?
如何测试数据正在发送并正确返回到服务器通过桩或嘲讽ember-data的提交功能,而无需重新测试已经测试了哪些ember-data?
最好在茉莉花!
答
在单元测试中,您绝对不应该使用真正的客户端服务器通信。通常你会模拟浏览器的XMLHttpRequest实现。
有一堆工具,如jasmine-fake-ajax或sinonjs。两者都覆盖浏览器的XHR实现并模拟服务器。所以你可以设定路线和应该返回的东西。两者都可以进行非常精细的调整,因此您可以检查是否为for类型,content-type或设置http响应代码。
{
setUp: function() {
this.xhr = sinon.useFakeXMLHttpRequest();
var requests = this.requests = [];
this.xhr.onCreate = function (xhr) {
requests.push(xhr);
};
},
tearDown: function() {
this.xhr.restore();
},
"test should fetch comments from server" : function() {
var callback = sinon.spy();
myLib.getCommentsFor("/some/article", callback);
assertEquals(1, this.requests.length);
this.requests[0].respond(200, { "Content-Type": "application/json" },
'[{ "id": 12, "comment": "Hey there" }]');
assert(callback.calledWith([{ id: 12, comment: "Hey there" }]));
}
}
什么意思是“被发送并正确返回”?你关心正确处理请求/回复吗? JSON内容格式化,按预期填充? – 2012-08-02 07:47:53