0
嗨,大家好我有麻煩來測試一個異步函數與一個服務器到一個服務器裏面。我正在使用摩卡和chai-as-promised。失敗的測試是:'返回正確的標題'我想我將不得不嘲笑fetch調用或者其他問題,或者問題是我調用了一個異步函數,並且在執行單元測試時,我不解決承諾。我不確定如何實現這一點。你可以幫幫我嗎?用mocha和chai測試異步方法
測試的功能是:
import React from 'react'
import Technologies from './Technologies'
import fetch from '../../core/fetch'
let title= 'Technologies'
export default {
path: '/technologies',
async action() {
const resp = await fetch('/graphql', {
method: 'post',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: '{technologies{name,icon,url}}',
}),
credentials: 'include',
})
let { data, } = await resp.json()
if (!data || !data.technologies) throw new Error('Failed to load technologies.')
return {
title:title,
component: <Technologies title={title} technologies={data.technologies} />,
}
},
}
而且我的測試:
describe('Route',() => {
it('has right path',() => {
expect(Route.path === '/technologies').to.be.true
})
it('return proper title',() => {
const title = 'Technologies'
expect(Route.action().title === title).to.be.true
})
})