Я новичок в тестировании, и я уверен, что есть кое-что, что я не делаю правильно
Я хочу протестировать Angular Service:
UserService.ts
import { Injectable } from '@angular/core';
import { API } from '@app-commons/core/API';
import { AuthService } from '@app-commons/services/AuthService';
import { CameraService } from '@app-commons/services/CameraService';
import { MessageController } from '@app-commons/controllers/MessageController';
@Injectable()
export class UserService {
public constructor(private api: API,
private auth: AuthService,
private cameraService: CameraService,
private message: MessageController) {}
public async changePicture() {
try {
const imgData = await this.cameraService.selectPicture();
const response = await this.api.put(`user/picture`, { picture: imgData });
}
catch (err) {
this.message.warningToast("Erreur lors de l'envoie de votre image, vous pouvez réessayer.", 3000);
}
this.message.okAlert('Super !', 'Votre image sera visible dans quelques instants !');
}
}
А вот мой тест
UserService.spec.ts
import { UserService } from './UserService';
const api: any = {
get: jest.fn(),
put: jest.fn()
};
const auth: any = {};
const cameraService: any = {
selectPicture: jest.fn()
};
const messageController: any = {
warningToast: jest.fn(),
okAlert: jest.fn()
};
const userService = new UserService(api, auth, cameraService, messageController);
describe('UserService : changeImage', () => {
test('should take a picture, upload it and display a message', async () => {
cameraService.selectPicture.mockReturnValueOnce('base64Image');
await userService.changePicture();
expect(messageController.okAlert).toBeCalled();
});
test('should not make an api call if camera error', async () => {
cameraService.selectPicture.mockImplementation(() => { throw new Error(); });
await userService.changePicture();
expect(api.put).not.toBeCalled();
});
});
первый тестовый этап, но у меня ошибка на втором.
● UserService : changeImage › should not use api if camera error
expect(jest.fn()).not.toBeCalled()
Expected mock function not to be called but it was called with:
["user/picture", {"picture": "base64Image"}]
69 | await userService.changePicture();
70 |
> 71 | expect(api.put).not.toBeCalled();
| ^
72 | // expect(messageController.warningToast).toBeCalled();
73 | });
74 |
Мы можем ясно видеть, что это во втором тесте, но он вызвал метод с параметрами, определенными в первом: 'base64Image' и .... Я совершенно не понимаю, почему ^^