gpt4 book ai didi

javascript - 在 Ember 中使用 Sinon 的假服务器

转载 作者:行者123 更新时间:2023-11-28 20:15:56 25 4
gpt4 key购买 nike

我想测试我的 Controller ,它向我的后端发出 ajax 调用。因此我想使用 Jasmine 和诗乃。 F为了用 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"
});
});
});

我的 Controller 看起来像这样:

 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
}
});
}
});

谁能告诉我如何运行它?

最佳答案

this.server.respondWith("GET", "http://myapi.com/comments/1",
[200, {"Content-Type": "application/json"},
'{"comment":{"id":1,"title":"ducks and ducks"}}']);

在我的例子中,url 应该是绝对的。

关于javascript - 在 Ember 中使用 Sinon 的假服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14243489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com