我有一个promise方法,在其中有if条件。当我嘲笑这个诺言方法。我只能返回一个值。我无法同时满足这两个条件。有人可以帮我吗?
import * as React from 'react';
import myLogs from '../service/myLogs';
export class MyMainComponent extends React.Component {
constructor(...args) {
super(...args);
this.getLogs();
}
getLogs() {
myLogs(5).then((logList) => {
if(logList.length > 0 ) {
// need to cover this.
} else {
// need to cover this.
}
});
}
render() { return null; }
}
service / myLogs.js
import axios from 'axios';
const myLogs = (loglength) => {
return axios.get("url).then((response) =>{return response})
}
export default myLogs
service / 模拟 /myLogs.js
const mockValue = [];
const mockValues = ["1", "2"];
const myLogs = (loglength) => {
// if i return "mockValue" then its covering else part.
// if i return "mockValues" then its covering if part.
// But i want to cover both. how can i mock to cover both.
return Promise.resolve(mockValue)
}
export default myLogs
test / MyMainComponent.js
jest.mock(../service/myLogs.js
// some other code..
答案 0 :(得分:0)
也许您可以使用参数loglength
来创建具有所需长度的数组。
赞:
const mockValue = (length) => Array(length).fill(3)
...
const myLogs = (loglength) => {
return Promise.resolve(mockValue(loglength))
}
export default myLogs
因此,您可以在此处指定每个测试用例所需的日志数量:)