Javascript Sinon.fakeServer响应model.fetch后主干模型未更新
我正在尝试为我们的主干应用程序编写一个测试套件。我们使用Jest作为测试框架,使用Sinon进行服务器模拟 一个示例测试如下所示 device.test.jsJavascript Sinon.fakeServer响应model.fetch后主干模型未更新,javascript,asynchronous,jestjs,sinon,synchronous,Javascript,Asynchronous,Jestjs,Sinon,Synchronous,我正在尝试为我们的主干应用程序编写一个测试套件。我们使用Jest作为测试框架,使用Sinon进行服务器模拟 一个示例测试如下所示 device.test.js describe('API integration', ()=>{ let response = {"data": { "attributes": { "created_at": "2018-06-14T07:05:14.919Z",
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);
});
});
我使用了一个例子来说明如何使用sinon测试主干模型(滚动到接近底部以查看fakeserver的使用情况),以及如何使用
根据我读到的内容,server.respond()
应该评估所有异步请求,因此模型应该同步更新。也就是说,我的期望是在server.respond
之后对device
的任何引用都应该是指一个post-fetchdevice
模型。我的期望没有达到-在调用server.respond()
之后,device.get('created_at')
仍然未定义。当我们的应用程序运行时,在生产环境中并非如此
以防万一,我试图将测试放入{success:function(){}
回调以及.then
和.done
,但是测试不会运行,也不会运行console.log()
或任何其他调试工作
如何使用Sinon.fakeServer测试异步方法,例如Backbone.model.fetch()
编辑:让我想知道是否有人调用了parse
——也许我的返回数据结构不正常。Parse确实被调用了,所以现在我怀疑这不是异步的问题,而是如何在响应
对象中构造数据的问题 答案是,我的模型的id
属性与我的API\u mock助手文件中响应对象的id
属性不匹配。它与异步无关