Проблема, которую я не понимаю насчет насмешек - PullRequest
0 голосов
/ 31 января 2019

Я новичок в тестировании, и я уверен, что есть кое-что, что я не делаю правильно

Я хочу протестировать 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' и .... Я совершенно не понимаю, почему ^^

1 Ответ

0 голосов
/ 31 января 2019

Функция Mock запоминает все сделанные ей вызовы.

В этом случае api.put вызывается во время первого теста .

Поскольку он не был очищен, он все еще сообщает, что он был вызван при проверке во втором тесте.

Используйте mockFn.mockClear(), чтобы очистить все предыдущие вызовы фиктивной функции:

test('should not make an api call if camera error', async () => {

    api.put.mockClear();  // clear the mock

    cameraService.selectPicture.mockImplementation(() => { throw new Error(); });

    await userService.changePicture();

    expect(api.put).not.toBeCalled();  // SUCCESS
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...