问题
I try to test a function in class who use a typeorm repository.
To write my testing class, I Use this exemple on github : https://github.com/jmcdo29/testing-nestjs/blob/master/apps/typeorm-sample/src/cat/cat.service.spec.ts
import { Injectable } from '@nestjs/common';
import { InjectRepository, getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Contact } from './contact.entity';
import { CreateContactDto } from './dto/createContact.dto';
import { ContactService } from './contact.service';
import { Test, TestingModule } from '@nestjs/testing';
const myContact: CreateContactDto = {email: "email@email.com", subject: "mySubject", content: "MyContent"};
describe('ContactService', () => {
let service: ContactService;
let repo: Repository<Contact>;
beforeEach(async() => {
const module: TestingModule = await Test.createTestingModule({
providers: [
ContactService,
{
provide: getRepositoryToken(Contact),
useValue: {
save: jest.fn().mockRejectedValue(myContact),
find: jest.fn().mockRejectedValue(myContact),
},
},
],
}).compile();
service = module.get<ContactService>(ContactService);
repo = module.get<Repository<Contact>>(getRepositoryToken(Contact))
});
describe('createContact', () => {
it('createContact Ok', async () => {
const myNewContact: CreateContactDto = {email: "email@email.com", subject: "mySubject", content: "MyContent"};
const result = await service.createContact(myNewContact);
expect(result.email).toEqual(myNewContact.email);
})
})
});
I don't see something wrong and i try to comment "repo = module.get..." and it's change nothing.
This is the service class :
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Contact } from './contact.entity';
import { CreateContactDto } from './dto/createContact.dto';
@Injectable()
export class ContactService {
constructor(
@InjectRepository(Contact)
private contactRepository: Repository<Contact>){}
async createContact(createContactDto: CreateContactDto) {
const contact = new Contact();
Object.assign(contact, createContactDto);
return contact.save();
}
async getContacts() {
return this.contactRepository.find();
}
}
And this is the error :
Thanks you for your help !
回答1:
Looks like it's related to new Contact()
. In the testing example you linked, the code uses this.repository.create()
instead of new Entity()
. Looking at TypeORM's BaseEntity source code, a lot of the static
and instance methods reference a
connectionobject, just like
save(). If you instead write your
createContact` like this:
async createContact(createContactDto: CreateContactDto) {
const contact = this.contactRepository.create(createContactDto);
await this.contactRepsitory.save(contact);
return contact;
}
you won't have any problems.
EDIT: Make sure to also add a mock into your test file for the repository.create
method. Otherwise, you'll get an error about create
not being a function.
来源:https://stackoverflow.com/questions/64728017/jest-connectionnotfounderror-connection-default-was-not-found