每当我尝试进行测试时,都未定义postMessagedata。
我在React组件中有这个postMessageData函数,我正在提交表单时调用它。 但是当我将整个组件导入到测试文件中时,我在测试文件中未定义它
postMessageData(msg){
let me = this;
axios.post('api endpoint', {
"entry": [
{
"sessionid": me.sessionid,
"tenantid": "d563cc487b",
"botid": "9523409096",
"channel": "Web",
"OpportunityID": "f63637d7e5",
"QuoteID": "f63637d7e5",
"OpportunityName":"service level",
"UserLegacyID": "f63637d7e5",
"UserFirstName": "botuser",
"RequestType": "change service level",
"messaging": [
{
"message": {
"messageId":"276",
"text": msg,
"timestamp": new Date()
},
"sender": {
"id": "1223",
"name": "rajdeep"
}
}
]
}
]
})
.then(function (response) {
console.log(response);
me.sessionid = response.data.sessionId;
//show loader
me.setState({ hide: true });
me.getResponseShow(response);
})
.catch(function (error) {
console.log(error);
});
}
我的测试代码是:
it('calls axios and return data', async () => {
const data = postMessageData('Hi');
expect(data.tenantid).toEqual('d563cc487b');
expect.assertions(1);
})
错误:
ReferenceError: postMessageData is not defined
53 |
54 | it('calls axios and return data', async () => {
> 55 | const data = postMessageData('Hi');
| ^
56 | expect(data.tenantid).toEqual('d563cc487b');
57 | expect.assertions(1);