我的规格表现怪异,因为当我单独运行测试时,它们通过了。但是,当我一起运行测试套件时,失败测试仍然继续使用成功axios模拟,而不是使用正确的失败http axios模拟。这导致我的测试失败。我是否缺少在代码的不同部分将2个模拟相互隔离的东西?
jobactions.js
db.aggregate()
spec.js
export const loadUnassignedJobs = (job_type) => {
if (!['unscheduled', 'overdue'].includes(job_type)) {
throw 'Job Type must be "unscheduled" or "overdue".';
}
return (dispatch) => {
dispatch({type: JobActionTypes.LOAD_UNASSIGNED_JOBS_STARTED, job_type });
return axios.get(defaults.baseapi_uri + 'jobs/' + job_type)
.then(function (response) {
dispatch(updateUnassignedJobs(response.data.jobs));
// handle success
})
.catch(function (error) {
// handle error
dispatch({ type: JobActionTypes.LOAD_UNASSIGNED_JOBS_FAILURE, error });
})
.then(function () {
// always executed
});
}
};
export const updateUnassignedJobs = (unassigned_jobs) => {
let unassigned_job_ids = [];
let jobs = {};
for (let job of unassigned_jobs) {
unassigned_job_ids.push(job.id);
jobs[job.id]=job;
}
return({
type: JobActionTypes.LOAD_UNASSIGNED_JOBS_SUCCESS,
jobs,
unassigned_job_ids,
});
};
答案 0 :(得分:1)
发现了问题!
成功案例的it()
块未使用done
回调导致afterEach()
moxios.uninstall()
被过早调用,并且在调用完成后未重置请求。解决此问题,现在所有测试通过。