【问题标题】:How can I test a class that has a constructor that takes other class objects? (jest)我如何测试一个有一个构造函数的类,该构造函数接受其他类对象? (笑话)
【发布时间】:2021-05-05 15:28:14
【问题描述】:

我正在尝试学习如何对特定任务进行单元测试。以下是我的一些文件:

//examples.service.ts
export class ExamplesService {
  constructor(
    @InjectModel(Example.name) private ExampleModel: Model<Example>,
    private readonly someService: SomeService,
    private readonly someOtherService: SomeOtherService
  ){}

  async findAll(): Promise<Example[]> { 
    return this.exampleModel.find().exec();
  }

  //... other class methods
}
//examples.controller.ts
@Controller('examples')
export class ExamplesController {
  constructor(private readonly examplesService: ExamplesService) {}
    
    @Get()
    async findAll(): Promise<Example[]> {
      return this.examplesService.findAll();
    }
    //... 

如果需要我创建一个还需要 SomeService 和 SomeOtherService 参数的新 ExamplesService 对象,我该如何测试 examples.service.ts 中的功能?我是否创建了所有这些对象的模拟?如果有,怎么做?

这是我目前所拥有的:

//examples.controller.spec.ts
jest.mock('./examples.service');
describe(ExamplesController, ()=> {
  let examplesController: ExamplesController;
  let examplesService: ExamplesService;

  beforeEach(async () => {
    const module = TestingModule = await Test.createTestingModule({
      controllers: [ExamplesController],
      providers: [ExamplesService],
    }).compile();
  
    examplesController = module.get<ExamplesController>(ExamplesController)
    examplesService = module.get<ExamplesService>(ExamplesService)
  })
  
  describe('findAll', () => {
    it('should return all the examples', async () => {
      let examples = await examplesService.findAll();
      expect(examples).resolves.toEqual('some result');
    })
  });
})

但是,在测试 findAll 时,它是不成功的,因为 examples 总是等于 undefined。

谢谢!

【问题讨论】:

    标签: typescript jestjs


    【解决方案1】:

    您需要模拟所有依赖项。模拟类有不同的方法。 在 jest 的网站https://jestjs.io/docs/en/es6-class-mocks 上有一些很好的例子。 不确定您要测试什么,但可以执行以下操作

        jest.mock('./Model');
        const someService = {
                someMethod: jest.fn().mockReturnValue(Math.random()),
                someOtherMethod: jest.fn().mockReturnValue({ }),
         };
        
        const someOtherService = {
              someMethod: jest.fn().mockReturnValue(Math.random()),
               
        };
           
        
        const examplesService = new ExamplesService(exampleModel as any, someService as any, someOtherService as any);
    

    【讨论】:

    • 谢谢!我有一个问题:为什么只模拟模型,而不是 someService 和 someOtherService 呢?我也在尝试测试 ExampleService 中的功能,例如findAll() 返回正确的值
    • 如您所见,我们没有为 someService 和 someOtherService 创建新对象。我们正在模拟 someService 的 2 个方法,然后在 ExampleService 中将 someService 模拟为 any。同样适用于 someOtherService。有多种方法可以模拟依赖项。我刚刚展示了两种类型。
    • 在您的测试中,您正在模拟测试类。我认为您需要为测试类创建对象并模拟其他依赖项。但我可能是错的。这里有一个类似的问题回答:stackoverflow.com/questions/52878055/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多