#mocking an entity, a service and a repository

6 messages · Page 1 of 1 (latest)

wheat badge
#

Hi, I'm trying to mock few dependencies of a service that i'm testing.
the dependencies as the constructor shows are:

export class CropStrainService extends  GenericEntityService <CropStrain, CropValidator> {
  constructor(
      @InjectRepository(CropStrain)
      protected cropStrainRepository: Repository<CropStrain>,
      @Inject(CropsConnectionService) private cropsConnectionService: CropsConnectionService
    ){
      super(cropStrainRepository);
      this.validator=new CropValidator(this);
    }

i've tried mocking the dependencies as shown here:


describe("CropType Service", () => {
    let cropTypeService: CropTypeService;
    const mockRepository = {};
    const mockConnectionService = {
        getStrainsByProject: jest.fn(projectId=>[{id:1,name:"harvesting",Crop:[],strains:[]}])
    };

    beforeEach(async () => {
        const module: TestingModule = await Test.createTestingModule({
            providers: [
                CropTypeService,
                {
                    provide: getRepositoryToken(CropType),
                    useValue: mockRepository
                },
                {
                    provide: CropsConnectionService,
                    useValue: mockConnectionService
                }
            ],
        }).compile();

        cropTypeService = module.get<CropTypeService>(CropTypeService);
    });

but the compiler doesn't read my mockings. what should be the right way to mock those dependencies?

sturdy abyss
fickle root
#

but the compiler doesn't read my mockings
What do you mean by this?

wheat badge
#

i'm getting the following error:

Nest can't resolve dependencies of the CropTypeService (CropTypeRepository, ?). Please make sure that the argument CropsConnectionService at index [1] is available in the RootTestModule context.

Potential solutions:
- If CropsConnectionService is a provider, is it part of the current RootTestModule?
- If CropsConnectionService is exported from a separate @Module, is that module imported within RootTestModule?
  @Module({
    imports: [ /* the Module containing CropsConnectionService */ ]
  })

  at TestingInjector.lookupComponentInParentModules (../node_modules/@nestjs/core/injector/injector.js:241:19)
  at TestingInjector.resolveComponentInstance (../node_modules/@nestjs/core/injector/injector.js:194:33)
  at TestingInjector.resolveComponentInstance (../node_modules/@nestjs/testing/testing-injector.js:16:45)
  at resolveParam (../node_modules/@nestjs/core/injector/injector.js:116:38)
      at async Promise.all (index 1)
  at TestingInjector.resolveConstructorParams (../node_modules/@nestjs/core/injector/injector.js:131:27)
  at TestingInjector.loadInstance (../node_modules/@nestjs/core/injector/injector.js:57:13)
  at TestingInjector.loadProvider (../node_modules/@nestjs/core/injector/injector.js:84:9)
      at async Promise.all (index 3)
fickle root
#

Can you show the CropTypeService class?

wheat badge
# fickle root Can you show the `CropTypeService` class?
import { forwardRef, Inject, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';
import { CreateCropTypeDto, UpdateCropTypeDto } from '../dto/cropType.dto';
import { CropType } from '../entities/cropType.entity';
import { GenericEntityService } from 'src/modules/infrastructures/services/service.generic';
import { CropValidator } from './crops.validator';
import { CropsConnectionService } from './crops.connection.service';


@Injectable()
export class CropTypeService extends  GenericEntityService <CropType, CropValidator> {
  constructor(
    @InjectRepository(CropType)
    protected repository: Repository<CropType>,
    //@Inject(forwardRef(()=>CropService)) private cropService: CropService
    @Inject(CropsConnectionService) private cropsConnectionService: CropsConnectionService
  ){
    super(repository);
    this.validator=new CropValidator(this)
  }