getItemForm 不调用第二个承诺
getItemForm not call second promise
我有一个调用 api.send 服务两次的函数,但是当我做测试期望它解决两个承诺时,它只 returns res1,而不是 res2,怎么可能我解决了吗?
我的函数:
getItemForm = () => {
this.api.send('filter', 'get', { lang: 'ES', filter: { id: this.item['id'] } }).then(res1 => {
this.item = res1['data'][0];
this.api.send('filter', 'get', { lang: 'EN', filter: { id: this.item['id'] } }).then(res2 => {
let itemEng = res2['data'][0];
});
});
};
我的测试:
it('should call api.send with res Eng', () => {
component.formDef = [{ field_id: 57, field_key: "name1", field_name: 'test1' }];
let res1 = { data: [{ id: 1, name: 'test1' }, { id: 2, name: 'test1' }] };
let res2 = { data: [{ id: 1, name: 'test2' }, { id: 2, name: 'test' }] };
component.item = let item = {id:1, count: 1, data:[{id: 1, tacticas: {95: [135]}}]};
let spy1 = spyOn(api, 'send').and.returnValues(Promise.resolve(res1), Promise.resolve(res2));
component.itemToForm();
expect(spy1).toHaveBeenCalled();
expect(spy1).toHaveBeenCalledTimes(2);
我强烈认为您应该更改函数的实现。你没有从中返回任何东西,不知道你是怎么得到 res1
的。做这样的事情:
getItemForm = async () => {
const res1 = await this.api.send('filter', 'get', { lang: 'ES', filter: { id: this.item['id'] } }),
res2 = await this.api.send('filter', 'get', { lang: 'EN', filter: { id: this.item['id'] } })
return {res1, res2};
};
我有一个调用 api.send 服务两次的函数,但是当我做测试期望它解决两个承诺时,它只 returns res1,而不是 res2,怎么可能我解决了吗?
我的函数:
getItemForm = () => {
this.api.send('filter', 'get', { lang: 'ES', filter: { id: this.item['id'] } }).then(res1 => {
this.item = res1['data'][0];
this.api.send('filter', 'get', { lang: 'EN', filter: { id: this.item['id'] } }).then(res2 => {
let itemEng = res2['data'][0];
});
});
};
我的测试:
it('should call api.send with res Eng', () => {
component.formDef = [{ field_id: 57, field_key: "name1", field_name: 'test1' }];
let res1 = { data: [{ id: 1, name: 'test1' }, { id: 2, name: 'test1' }] };
let res2 = { data: [{ id: 1, name: 'test2' }, { id: 2, name: 'test' }] };
component.item = let item = {id:1, count: 1, data:[{id: 1, tacticas: {95: [135]}}]};
let spy1 = spyOn(api, 'send').and.returnValues(Promise.resolve(res1), Promise.resolve(res2));
component.itemToForm();
expect(spy1).toHaveBeenCalled();
expect(spy1).toHaveBeenCalledTimes(2);
我强烈认为您应该更改函数的实现。你没有从中返回任何东西,不知道你是怎么得到 res1
的。做这样的事情:
getItemForm = async () => {
const res1 = await this.api.send('filter', 'get', { lang: 'ES', filter: { id: this.item['id'] } }),
res2 = await this.api.send('filter', 'get', { lang: 'EN', filter: { id: this.item['id'] } })
return {res1, res2};
};