微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用 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 举报,一经查实,本站将立刻删除。