2
我想測試我的控制器,它對我的後端進行ajax調用。爲此我想用茉莉花和頌尼。 Ffor僞造我的後端服務器興農我想是這樣的:Ember中有Sinon的假服務器
describe("fake server", function() {
var server;
beforeEach(function() {
this.server = sinon.fakeServer.create();
});
afterEach(function() {
this.server.restore();
});
it("calls callback with deserialized data", function() {
var callback = sinon.spy();
this.server.respondWith("GET", "/comments/1",
[200, {"Content-Type": "application/json"},
'{"comment":{"id":1,"title":"ducks and ducks"}}']);
commentController = App.CommentController.create();
//commentController.bind('getComment', callback);
commentController.getComment();
this.server.respond();
expect(callback.called).toBeTruthy();
expect(callback.getCall(0).args[0].attributes)
.toEqual({
id: "1",
title: "ducks and ducks"
});
});
});
我的控制器看起來是這樣的:
App.CommentController = Ember.Controller.extend({
getComment: function() {
$.ajax({
url: 'http://myapi/comments/' + id,
//...
error: function(jqXHR, textStatus){
this.set("error",true);
//do something
},
success: function(data) {
this.set("error",false);
//do something else
}
});
}
});
有人能告訴我如何我得到運行此?
您是否考慮過使用Ember Data的FixtureAdapter?這是一個[小提琴](http://jsfiddle.net/schawaska/4HFLw/)我分叉(使用版本10),所以你可以看到它是如何工作的 – MilkyWayJoe
是的,我做到了。但即使在那裏,我也有異步的後端調用,我想用sinon假服務器來模擬。 –