Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 执行测试时,不调用该函数_Typescript_Testing_Jestjs - Fatal编程技术网

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

Typescript 执行测试时,不调用该函数,typescript,testing,jestjs,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

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

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();
        });
        ....
    });
});
从'@nestjs/common'导入{Controller,Post,Body,Get,Put,Delete,Param};
从“./todos.service”导入{TodosService};
从“./Todos.entity”导入{Todos};
const message={message:'未找到条目'};
@控制器('待办事项')
将类导出到OSCONTROLLER{
构造函数(私有todosService:todosService){}
....
@放置(“/update”)
异步updateTodo(@Body()todos:todos):承诺{
return(等待this.todosService.getTodo(todos.id))?this.todosService.updateTodo(todos):消息;
}
....
}
我想为这个函数写一个测试

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();
        });
        ....
    });
});
从'@nestjs/testing'导入{Test,TestingModule};
从“./todos.controller”导入{TodosController};
从“./todos.service”导入{TodosService};
从“./Todos.entity”导入{Todos};
jest.mock('./todos.service');
描述('Todos控制器',()=>{
让todosController:todosController;
让我们去做服务:去做服务;
beforeach(异步()=>{
常量模块:TestingModule=等待测试。createTestingModule({
控制器:[TodosController],
提供者:[TodosService],
}).compile();
todosController=module.get(todosController);
todosService=module.get(todosService);
});
描述('TodosController()',()=>{
....
它('updateTodo()函数应该调用',()=>{
spyOn(todosService,'updateTodo')。和.callThrough();
const data:Todos={id:1,标题:“新标题”,作者:“姓名1”,日期:新日期('2019-11-18');
todosController.updateTodo(数据);
expect(todosService.updateTodo).tohavebeencall();
});
....
});
});
当我运行测试时,我得到一个错误

● Todos控制器›TODOSCONTOLLER()›updateTodo()函数应调用

expect(spy).tohavebeencall()
预期呼叫数:>=1
收到的呼叫数:0
55 | const data:Todos={id:1,标题:“新标题”,作者:“姓名1”,日期:新日期('2019-11-18');
56 | todosController.updateTodo(数据);
>57 | expect(todosService.updateTodo).tohavebeencall();
|                                             ^
58 |         });
59 | 
60 |//它('deleteTodo()函数应调用',()=>{
对象。(todos/todos.controller.spec.ts:57:45)
我可能犯了什么错误?因为所有其他功能都可以工作


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

由于updateTodo是一个异步函数,请等待它的承诺得到解决,然后再“预期”响应

尝试以下方法:

const response = await  todosController.updateTodo(data);
//Then run your checks
否则,expect命令甚至会在承诺解析之前执行