[英]Test case failing
I am trying to test the following function using jest:我正在尝试使用 jest 测试以下功能:
fetchContent() {
fetch(`${this.contentApi}?id=${this.id}`)
.then(data => data.json())
.then(response => {
this.content = response;
console.log('===this.content====', this.content);
console.log('===Response====', response);
console.log('===This====', this);
this.assignContent();
})
.catch(error => {
throw Error(error);
});
}
In order to write test cases for this function, I mocked the window.fetch
function, contentApi
, id
and the assignContent
function.为了为此函数编写测试用例,我模拟了
window.fetch
函数、 contentApi
、 id
和assignContent
函数。 Then, I tried to call this function inside of my test case by mocking all the necessary functions and variables.然后,我尝试通过模拟所有必要的函数和变量在我的测试用例中调用这个函数。
Here is a snippet of the test case:这是测试用例的一个片段:
it('should fetch and assign content', () => {
obj.assignContent = jest.fn();
obj.contentApi = 'abc.xyz';
obj.id = 'dxp';
window.fetch = jest.fn().mockImplementation(url => {
if(url === 'abc.xyz?id=dxp') {
return Promise.resolve({
json: () => { return 'abc'; }
})
}
else {
return Promise.reject(new Error());
}
});
obj.fetchContent();
console.log('===OBJ====', obj);
// expect(obj.content).toEqual('abc');
// expect(obj.assignContent).toBeCalled();
});
It is failing and it is neither setting content as 'abc'
, nor it is calling assignContent()
.它失败了,它既没有将内容设置为
'abc'
,也没有调用assignContent()
。
console.log src\dxp-container.spec.tsx:57
===OBJ==== Container {
initialAssignmentDone: false,
assignContent:
{ [Function: mockConstructor]
_isMockFunction: true,
getMockImplementation: [Function],
mock: [Getter/Setter],
mockClear: [Function],
mockReset: [Function],
mockReturnValueOnce: [Function],
mockReturnValue: [Function],
mockImplementationOnce: [Function],
mockImplementation: [Function],
mockReturnThis: [Function],
mockRestore: [Function] },
contentApi: 'abc.xyz',
id: 'dxp' }
console.log src\dxp-container.tsx:24
===this.content==== abc
console.log src\dxp-container.tsx:25
===Response==== abc
console.log src\dxp-container.tsx:26
===This==== Container {
initialAssignmentDone: false,
assignContent:
{ [Function: mockConstructor]
_isMockFunction: true,
getMockImplementation: [Function],
mock: [Getter/Setter],
mockClear: [Function],
mockReset: [Function],
mockReturnValueOnce: [Function],
mockReturnValue: [Function],
mockImplementationOnce: [Function],
mockImplementation: [Function],
mockReturnThis: [Function],
mockRestore: [Function] },
contentApi: 'abc.xyz',
id: 'dxp',
content: 'abc' }
Your mockend fetch
function is still asynchronous (it returns a Promise) so you still have to "wait" for it before you can access the "loaded" data.您的 mockend
fetch
函数仍然是异步的(它返回一个 Promise),因此您仍然必须“等待”它才能访问“加载”的数据。
fetchContent() {
return fetch(`${this.contentApi}?id=${this.id}`)
...
}
obj.fetchContent().then(() => {
console.log('===OBJ====', obj);
})
Of course you'll have to tell Jest to wait for the async code so you'll want to also return obj.fetchContent()...
.当然,您必须告诉 Jest 等待异步代码,因此您还需要
return obj.fetchContent()...
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.