(开玩笑)ConnectionNotFoundError:连接“默认";没找到 [英] (Jest) ConnectionNotFoundError: Connection "default" was not found

查看:603
本文介绍了(开玩笑)ConnectionNotFoundError:连接“默认";没找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在使用typeorm存储库的类中测试一个函数.

I try to test a function in class who use a typeorm repository.

要编写测试类,请在github上使用以下示例:

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);
        })
    })
});

我没有看到任何错误,我尝试评论"repo = module.get ...".它什么也没改变.

I don't see something wrong and i try to comment "repo = module.get..." and it's change nothing.

这是服务类别:

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();
  }
}

这是错误:

感谢您的帮助!

推荐答案

好像与new Contact()有关.在您链接的测试示例中,代码使用this.repository.create()而不是new Entity().查看 TypeORM的BaseEntity源代码staticinstance methods reference a connection object, just like save(). If you instead write your createContact`如下:

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 likesave(). 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;
  }

您不会有任何问题.

编辑:请确保还将repository.create方法的模拟内容添加到您的测试文件中.否则,您会收到关于create不是函数的错误.

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.

这篇关于(开玩笑)ConnectionNotFoundError:连接“默认";没找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆