如何测试api.send函数的第二层
How to test the second level of the api.send function
我正在测试一个 returns 承诺的函数,然后再次调用相同的方法,但我不知道如何访问第二级调用。
我的函数
itemToForm = () => {
this.api.send(this.component, 'get', { lang: 'ES', filter: { id: this.item['id'] } }).then(resEsp => {
this.item = resEsp['data'][0];
this.api.send(this.component, 'get', { lang: 'EN', filter: { id: this.item['id'] } }).then(res => {
let itemEng = res['data'][0]
let fields = this.formDef
.map(register => register
.filter(field => field['register_table'].indexOf('traduction') !== -1)
.map(field => field['field_name'])
).filter(register => register.length);
fields = fields.length ? fields[0] : [];
if (itemEng)
this.item = Object.keys(itemEng)
.reduce((obj, key) => {
obj[key] = this.item[key];
if (fields.indexOf(key) !== -1) {
obj[key + '_eng'] = itemEng[key];
}
return obj
}, {})
if (this.item) {
this.setForm();
}
})
})
}
我有第一级的正确测试,但是当我模拟第二级时,我不知道如何为这个调用配置 spyOn。
我的测试:
describe('itemToForm()', () => {
it('should call api.send with resEsp', () => {
let item = { id: 1, name: 'test' };
component.component = 'Events';
let resEsp = {
data: [
{ id: 1, name: 'test1' },
{ id: 2, name: 'test2' },
{ id: 3, name: 'test3' }
]
};
component.item = item;
//let spy1 = spyOn(api, 'send').and.returnValue(Promise.resolve(resEsp));
let spy1 = spyOn(api, 'send').and.returnValue(Promise.resolve(resEsp));
component.itemToForm();
expect(spy1).toHaveBeenCalled();
expect(spy1).toHaveBeenCalledTimes(1);
expect(spy1).toHaveBeenCalledWith('Events', 'get', { lang: 'ES', filter: { id: 1 } });
});
it('should call api.send with res Eng', () => {
let itemEng = { id: 1, name: 'test', lang: 'EN' }
component.component = 'Events';
let resEsp = {data: [{ id: 1, name: 'test1' }, { id: 2, name: 'test2' }, { id: 3, name: 'test3' }]};
let res = {data: [{ id: 1, name: 'test1' }, { id: 2, name: 'test2' }, { id: 3, name: 'test3' }]};
component.item = itemEng;
//let spy1 = spyOn(api, 'send').and.returnValue(Promise.resolve(resEsp));
let spy1 = spyOn(api, 'send').and.returnValue(Promise.resolve(resEsp)).and.returnValue(Promise.resolve(res));
component.itemToForm();
expect(spy1).toHaveBeenCalled();
});
});
使用 return 个值代替 return 个序列中的多个值。
let spy1 = spyOn(api, 'send').and.returnValues(Promise.resolve(resEsp), Promise.resolve(res));
https://jasmine.github.io/2.5/introduction#section-Spies:_%3Ccode%3Eand.returnValues%3C/code%3E
我正在测试一个 returns 承诺的函数,然后再次调用相同的方法,但我不知道如何访问第二级调用。
我的函数
itemToForm = () => {
this.api.send(this.component, 'get', { lang: 'ES', filter: { id: this.item['id'] } }).then(resEsp => {
this.item = resEsp['data'][0];
this.api.send(this.component, 'get', { lang: 'EN', filter: { id: this.item['id'] } }).then(res => {
let itemEng = res['data'][0]
let fields = this.formDef
.map(register => register
.filter(field => field['register_table'].indexOf('traduction') !== -1)
.map(field => field['field_name'])
).filter(register => register.length);
fields = fields.length ? fields[0] : [];
if (itemEng)
this.item = Object.keys(itemEng)
.reduce((obj, key) => {
obj[key] = this.item[key];
if (fields.indexOf(key) !== -1) {
obj[key + '_eng'] = itemEng[key];
}
return obj
}, {})
if (this.item) {
this.setForm();
}
})
})
}
我有第一级的正确测试,但是当我模拟第二级时,我不知道如何为这个调用配置 spyOn。
我的测试:
describe('itemToForm()', () => {
it('should call api.send with resEsp', () => {
let item = { id: 1, name: 'test' };
component.component = 'Events';
let resEsp = {
data: [
{ id: 1, name: 'test1' },
{ id: 2, name: 'test2' },
{ id: 3, name: 'test3' }
]
};
component.item = item;
//let spy1 = spyOn(api, 'send').and.returnValue(Promise.resolve(resEsp));
let spy1 = spyOn(api, 'send').and.returnValue(Promise.resolve(resEsp));
component.itemToForm();
expect(spy1).toHaveBeenCalled();
expect(spy1).toHaveBeenCalledTimes(1);
expect(spy1).toHaveBeenCalledWith('Events', 'get', { lang: 'ES', filter: { id: 1 } });
});
it('should call api.send with res Eng', () => {
let itemEng = { id: 1, name: 'test', lang: 'EN' }
component.component = 'Events';
let resEsp = {data: [{ id: 1, name: 'test1' }, { id: 2, name: 'test2' }, { id: 3, name: 'test3' }]};
let res = {data: [{ id: 1, name: 'test1' }, { id: 2, name: 'test2' }, { id: 3, name: 'test3' }]};
component.item = itemEng;
//let spy1 = spyOn(api, 'send').and.returnValue(Promise.resolve(resEsp));
let spy1 = spyOn(api, 'send').and.returnValue(Promise.resolve(resEsp)).and.returnValue(Promise.resolve(res));
component.itemToForm();
expect(spy1).toHaveBeenCalled();
});
});
使用 return 个值代替 return 个序列中的多个值。
let spy1 = spyOn(api, 'send').and.returnValues(Promise.resolve(resEsp), Promise.resolve(res));
https://jasmine.github.io/2.5/introduction#section-Spies:_%3Ccode%3Eand.returnValues%3C/code%3E