1

我正在尝试为我的 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()它似乎每次都会中断。

4

0 回答 0