我通过API获取真实数据,然后像这样判断响应是成功还是失败.
{ message: "fail" }
如果消息是失败,我想强制测试失败,我还想判断状态是否正常.即使使用抛出新错误,测试仍然是成功的.
我的代码是这样的
describe('fetch Data', () => {
beforeEach(() => {
cy.visit('http://localhost:3000');
});
it('Fetch Data', () => {
fetch('https://mocki.io/v1/82b25ba1-615a-4ff6-8c5d-c9f464f8c843').then((response) => {
if (!response.ok) {
throw new Error('Something went wrong!');
} else {
return response.json();
}
}).then((res) => {
expect(res.message).to.contain('success');
cy.log('result => ', res);
}).catch((err) => {
throw new Error(err);
});
});
});