我有这种奇怪的情况,我不理解。
在vue组件中,我使用“存储库/服务”进行API调用。它使用axios:
我将其导入组件:
import { RepositoryFactory } from "AMComponents/repositories/repository-factory";
const Contactsrepository = RepositoryFactory.get("contacts");
在方法addContact
中,我这样使用它:
现在, 这是我的测试方式:
test("addContact",() => {
const newContact = {
"name": "Hulk Hogan","email": "hulk@hogan.com","phone": "","additional_information": "Hulkamania is running wild,Brother","type": "advertiser",};
Contactsrepository.createContact = jest.fn()
.mockResolvedValue({
data: {
response: {
contact_information: [...contacts,newContact],passport_privileges: {},},})
.mockRejectedValue({
response: {
status: 400,data: {
messages: ["mocked error"],});
window.toastr = {
success: jest.fn(),error: jest.fn(),};
wrapper.vm.addContact(newContact);
expect(wrapper.vm.saving).toBe(true);
expect(Contactsrepository.createContact).toHaveBeenCalled();
flushPromises()
.then(() => {
expect(1).toBe(2); // <-- here is where I expect my test to fail
expect(wrapper.vm.saving).toBe(false);
expect(window.toastr.error).toHaveBeenCalled();
expect(wrapper.emitted("update")[0][0]).toEqual([...contacts,newContact]);
})
.catch((error) => {
throw Error(error)
})
});
我期望测试失败,因为我使用断言expect(1).toBe(2)
。
相反,我得到这样的结果:
我花了大约5个小时尝试不同的解决方案来使这项工作顺利进行。
您能告诉我这是怎么回事吗?或者至少将我指向正确的方向。