【发布时间】:2012-06-05 18:10:07
【问题描述】:
我正在尝试使用 sinon.fakeServer 或 sinon.useFakeXMLHttpRequest 和 require.js 模拟 Backbone.Model.fetch 请求。
这是我无法正常工作的代码片段 (1)
我的问题是:
如何使用 sinon.fakeServer 获取夹具数据?
请在这段代码末尾的两个cmets。
附:
如果我发出 fetch 请求并评论有关 sinon.fakeServer 的代码,它会向服务器发出 get 请求。
如果我使用 sinon.fakeServer 发出 get 请求,它不会获取任何东西(服务器和夹具)
(1)
define([
'js/models/myModel',
'js/spec/fixtures/myModel.fixture'
], function (MyModel) {
beforeEach(function () {
this.myModel = new MyModel();
console.log("fixtures", this.fixtures.Task, this);
this.fixture = this.fixtures.Task.valid;
this.fixtureTask = this.fixture.response;
this.server = sinon.fakeServer.create();
this.server.respondWith(
"GET",
Routing.generate("api_get_tasks"),
JSON.stringify(this.fixture)
);
});
afterEach(function () {
this.server.restore();
});
it("should make the correct request", function() {
this.server.respond();
this.feeds.fetch();
console.log(this.fixture); // this response is OK
console.log(this.myModel.attributes); // it does not take the value from this.fixture
console.log("fixtures", this.fixtures.Task, this); // see the picture below
});
});
【问题讨论】:
-
console.log("fixtures", this.fixtures.Task, this);输出是什么? -
@user1248256 我回复你,附上一张照片。
标签: javascript testing backbone.js jasmine sinon