使用 Jest 自定义存储库 (CassandraDB) 进行 NestJS 测试

问题描述

我正在尝试为我的 nodeJS 项目测试驱动程序/存储库的代码

import { Injectable,OnModuleInit } from '@nestjs/common';
import { mapping,types } from 'cassandra-driver';
import { Products } from './poducts.model';
import { CassandraService } from '../database/cassandra/cassandra.service';
import Uuid = types.Uuid;

@Injectable()
export class ProductsRepository implements OnModuleInit {
  constructor(private cassandraService: CassandraService) {}

  productsMapper: mapping.modelmapper<Products>;

  onModuleInit() {
    const mappingOptions: mapping.MappingOptions = {
      models: {
        Products: {
          tables: ['products'],mappings: new mapping.UnderscoreCqlToCamelCaseMappings(),},};

    this.productsMapper = this.cassandraService
      .createMapper(mappingOptions)
      .forModel('Products');
  }

  async getProducts() {
    return (await this.productsMapper.findAll()).toArray(); // <-----Breaks here with findAll()
  }
}

我正在尝试写这样的东西:

  describe('product repository get all',() => {
    it('calls the repository get all',async () => {
      const await productsRepository.getProducts();
      expect().DoSomething()
    });
  });

这是我得到的错误

Cannot read property 'findAll' of undefined

我如何使用 Jest 完成有意义的完整测试以获得正确的代码覆盖率?

当我试图用玩笑来监视 this.products.Mapper.findAll() 时,它似乎每次都会中断。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)