Skip to content

Testing With Database #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
psaunders opened this issue Mar 23, 2018 · 3 comments
Closed

Testing With Database #523

psaunders opened this issue Mar 23, 2018 · 3 comments

Comments

@psaunders
Copy link

psaunders commented Mar 23, 2018

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request

Current behavior

The documentation is pretty weak on integration testing but even a read through of all the issues had no solution that worked for me.

Nest can't resolve dependencies of the RoundService (?). Please verify whether [0] argument is available in the current context.

A TypeOrm entity

import {Entity, PrimaryGeneratedColumn, BaseEntity} from "typeorm";

@Entity('round')
export class Round extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;
}

A Service

import { Component } from '@nestjs/common';
import { Round } from './round.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

@Component()
export class RoundService
{
  constructor(
    @InjectRepository(Round)
    private readonly repo: Repository<Round>
  ) { }

 someFunction(){
    return "I exist";
 }

A test

import { Test, TestingModule } from "@nestjs/testing";
import { RoundService } from "../round.service";
import { TypeOrmModule } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { Round } from "../round.entity";

describe('RoundService', () => {

  let service: RoundService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [TypeOrmModule.forRoot()],
      components: [RoundService, {
        provide: RoundRepository,
        useClass: Repository
      }
      ]
    }).compile();

    service = module.get<RoundService>(RoundService);
  });

  describe('run', () => {
    it('should be able to run some function', async () => {
      service.someFunction();
    });
  });
}

Expected behavior

Test to new up the service successfully.

Minimal reproduction of the problem with instructions

#363 has answers from @VinceOPS that would lead me to believe this would work, but obviously "RoundRepository" doesn't exist because it's called "Round". If you can help me out with this I'll submit a PR for the docs, I think injecting more complex dependencies really illustrates the structure of a NestJS application well.

What is the motivation / use case for changing the behavior?

Environment


Nest version: 4.6.6
@pumano
Copy link
Contributor

pumano commented Mar 25, 2018

Also very important for me ( in case of MongooseModule)

@kamilmysliwiec
Copy link
Member

I'm on it! Will update docs soon. Closing due to #363 duplicate.

@lock
Copy link

lock bot commented Sep 25, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants