typescript - 执行测试时,不调用该函数

标签 typescript testing jestjs

我有一个控件,其中有一个任务更新功能。

import { Controller, Post, Body, Get, Put, Delete, Param } from '@nestjs/common';
import { TodosService } from './todos.service';
import { Todos } from './todos.entity';

const message = { message: 'Entry not found' };

@Controller('todos')
export class TodosController {
    constructor(private todosService: TodosService) {}
    ....
    @Put('/update')
    async updateTodo(@Body() todos: Todos): Promise<void | { message: string }> {
        return (await this.todosService.getTodo(todos.id)) ? this.todosService.updateTodo(todos) : message;
    }
   ....
}

我想为此功能编写一个测试。

import { Test, TestingModule } from '@nestjs/testing';
import { TodosController } from './todos.controller';
import { TodosService } from './todos.service';
import { Todos } from './todos.entity';

jest.mock('./todos.service');

describe('Todos Controller', () => {
    let todosController: TodosController;
    let todosService: TodosService;

    beforeEach(async () => {
        const module: TestingModule = await Test.createTestingModule({
            controllers: [TodosController],
            providers: [TodosService],
        }).compile();

        todosController = module.get<TodosController>(TodosController);
        todosService = module.get<TodosService>(TodosService);
    });

    describe('TodosController()', () => {
        ....
        it('updateTodo() function should call', () => {
            spyOn(todosService, 'updateTodo').and.callThrough();
            const data: Todos = { id: 1, title: 'New title', author: 'Name 1', date: new Date('2019-11-18') };
            todosController.updateTodo(data);
            expect(todosService.updateTodo).toHaveBeenCalled();
        });
        ....
    });
});

当我运行测试时,出现错误。

● Todos Controller › TodosController() › updateTodo() function should call

expect(spy).toHaveBeenCalled()

Expected number of calls: >= 1
Received number of calls:    0

  55 |             const data: Todos = { id: 1, title: 'New title', author: 'Name 1', date: new Date('2019-11-18') };
  56 |             todosController.updateTodo(data);
> 57 |             expect(todosService.updateTodo).toHaveBeenCalled();
     |                                             ^
  58 |         });
  59 | 
  60 |         // it('deleteTodo() function should call', () => {

  at Object.<anonymous> (todos/todos.controller.spec.ts:57:45)

我的错误是什么?因为所有其他功能都有效。

经过一些尝试,我注意到当我删除三元运算符时,所有测试都成功通过了。我不知道这可能与什么有关。

最佳答案

因为 updateTodo 是一个异步函数,在“期待”响应之前等待它的 Promise 解决。

尝试这样的事情:

const response = await  todosController.updateTodo(data);
//Then run your checks

否则,即使在 promise 解析之前,expect 命令也会被执行。

关于typescript - 执行测试时,不调用该函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58799563/

相关文章:

javascript - 使用 RegEx 替代条件语句

reactjs - event.currentTarget.name 返回为 currentTarget

typescript - 安装 webpack-dev-server npm 包时 typescript 编译不安全

docker - 如何测试角色?

javascript - 库包装器 React 组件,如何使用 Enzyme 查找 DOM 子项?

typescript - 回调中对象可能为 null

android - 如何组合不同的testInstrumentationRunner

testing - 无法使用 Autohotkey 找到文本

jestjs - 如何使用 enzyme 测试事件处理程序

javascript - React Redux 应用程序中 Windows 10 上的 Jest "No tests found, exiting with code 1"错误