Backbone Sinon.fakeServer 响应 model.fetch 后模型未更新
Backbone Models not updating after Sinon.fakeServer responds to model.fetch
我正在尝试为我们的 Backbone 应用编写测试套件。我们使用 Jest 作为测试框架,使用 Sinon 作为服务器模拟。
示例测试看起来像
device.test.js
describe('API integration', ()=>{
let response = {"data": {
"attributes": {
"created_at": "2018-06-14T07:05:14.919Z",
},
"id": "234",
"type": "device"
}};
let server = sinon.fakeServer.create();
afterAll(() => {
server.restore();
});
let device = new Device({
id: '234'
created_at: null,
agent_code: null,
device_code: null,
});
server.respondWith('GET',
'api/device/234',
[200, {"Content-Type": "application/json"},
JSON.stringify(response)]);
device.fetch();
server.respond();
it('fetches to the right URL', ()=>{
// these both pass
expect(server.requests[0].method === 'GET');
expect(server.requests[0].url === 'api/device/234');
});
it('updates its fields properly after a fetch', ()=>{
// this test fails
expect(device.get('created_at')).toBe(response.attributes.created_at);
});
});
我正在使用 this article as an example of how to test backbone models with sinon (scroll towards nearly the bottom for fakeserver usage) as well as using the sinon fakeserver docs。
根据我的阅读,server.respond()
应该评估所有异步请求,因此模型应该同步更新。也就是说,我的期望是 server.respond
之后对 device
的任何引用都应该指的是 post-fetch device
模型。我的期望没有达到 - 在调用 server.respond()
后,device.get('created_at')
仍然是 undefined
。当我们的应用程序 运行ning 时,生产环境中情况并非如此。
为了以防万一,我尝试将测试放入 {success: function(){}}
回调以及 .then
和 .done
,但测试不会 运行,console.log()
或任何其他调试工作也不会。
如何在 Jest 中使用 Sinon.fakeServer 测试异步方法,例如 Backbone.model.fetch()
?
编辑:This answer 让我想知道是否 parse
甚至被调用了——也许我正在异常地构建我的 return 数据。 Parse 确实被调用了,所以现在我怀疑这不是异步问题,而是我如何在 response
对象中构建数据的问题。
答案是我的模型的 id
属性 与我的 API_MOCKS 助手的响应对象的 id
属性 不匹配文件。它与异步无关。
我正在尝试为我们的 Backbone 应用编写测试套件。我们使用 Jest 作为测试框架,使用 Sinon 作为服务器模拟。
示例测试看起来像
device.test.js
describe('API integration', ()=>{
let response = {"data": {
"attributes": {
"created_at": "2018-06-14T07:05:14.919Z",
},
"id": "234",
"type": "device"
}};
let server = sinon.fakeServer.create();
afterAll(() => {
server.restore();
});
let device = new Device({
id: '234'
created_at: null,
agent_code: null,
device_code: null,
});
server.respondWith('GET',
'api/device/234',
[200, {"Content-Type": "application/json"},
JSON.stringify(response)]);
device.fetch();
server.respond();
it('fetches to the right URL', ()=>{
// these both pass
expect(server.requests[0].method === 'GET');
expect(server.requests[0].url === 'api/device/234');
});
it('updates its fields properly after a fetch', ()=>{
// this test fails
expect(device.get('created_at')).toBe(response.attributes.created_at);
});
});
我正在使用 this article as an example of how to test backbone models with sinon (scroll towards nearly the bottom for fakeserver usage) as well as using the sinon fakeserver docs。
根据我的阅读,server.respond()
应该评估所有异步请求,因此模型应该同步更新。也就是说,我的期望是 server.respond
之后对 device
的任何引用都应该指的是 post-fetch device
模型。我的期望没有达到 - 在调用 server.respond()
后,device.get('created_at')
仍然是 undefined
。当我们的应用程序 运行ning 时,生产环境中情况并非如此。
为了以防万一,我尝试将测试放入 {success: function(){}}
回调以及 .then
和 .done
,但测试不会 运行,console.log()
或任何其他调试工作也不会。
如何在 Jest 中使用 Sinon.fakeServer 测试异步方法,例如 Backbone.model.fetch()
?
编辑:This answer 让我想知道是否 parse
甚至被调用了——也许我正在异常地构建我的 return 数据。 Parse 确实被调用了,所以现在我怀疑这不是异步问题,而是我如何在 response
对象中构建数据的问题。
答案是我的模型的 id
属性 与我的 API_MOCKS 助手的响应对象的 id
属性 不匹配文件。它与异步无关。