如何模拟typeORM存储库

时间:2019-07-10 20:40:02

标签: typescript unit-testing mocking repository typeorm

我想编写一些测试并模拟typeORM存储库。  尝试了几件事,但仍无法解决...(例如.. @InjectRepository)...有什么想法吗?

import { AfterRoutesInit, Service } from '@tsed/common';
import { TypeORMService } from '@tsed/typeorm';
import { Connection, Repository } from 'typeorm';
import { DbAccount } from '../entity/DbAccount';
import { DbAddress } from '../entity/DbAddress';
import { Account } from '../models/Account';
import { Address } from '../models/address';
import { Pageable } from '../models/api_common/Pageable';

@Service()
export default class AccountService implements AfterRoutesInit {
    private repository: Repository<DbAccount>;
    private addressRepository: Repository<DbAddress>;

    constructor(private typeORMService: TypeORMService) { }

    $afterRoutesInit(): void | Promise<any> {
        const connection: Connection = this.typeORMService.get('default');
        this.repository = connection.getRepository(DbAccount);
        this.addressRepository = connection.getRepository(DbAddress);
    }

    async delete(accountId: string): Promise<void> {
        await this.repository.delete(accountId);

        return;
    }

}

2 个答案:

答案 0 :(得分:0)

这就是我的方法。

import sinon from 'sinon';
import {ConnectionManager, Repository, Connection} from 'typeorm';

describe('Auth service Unit tests:', () => {
   const sandbox = sinon.createSandbox()
   beforeEach(() => {
    sandbox.stub(ConnectionManager.prototype, 'get').returns({
      getRepository: sandbox.stub().returns(sinon.createStubInstance(Repository))
    } as unknown as Connection)
   })

   afterEach(() => {
     sandbox.restore();
   })

   it('login() with valid username and password', (done) => {
      // Test here 
   }) 
})

答案 1 :(得分:-1)

如果您的问题解决了,我没有,但是这里有一个解决方案:

import {TestContext} from "@tsed/testing";
import {TypeORMService} from "@tsed/typeorm";
import {expect} from "chai";
import * as Sinon from "sinon";
import {AccountService} from "./AccountService";
import {DbAccount} from "./DbAccount";
import {DbAddress} from "./DbAddress";

describe("AccountService", () => {
  before(() => TestContext.create());
  after(() => TestContext.reset());
  describe("afterRoutesInit()", () => {
    it("should initialise repository when afterRoutesInit event is emitted", () => {
      // GIVEN
      const connection = {
        getRepository: Sinon.stub()
      };
      const typeORMService = {
        get: Sinon.stub().returns(connection)
      };
      const dbAccountRepository = {name: "dbAccountRepository"};
      const dbAddressRepository = {name: "dbAddressRepository"};

      connection.getRepository
        .withArgs(DbAccount).returns(dbAccountRepository);
      connection.getRepository
        .withArgs(DbAddress).returns(dbAddressRepository);

      const service = TestContext.invoke<AccountService>(AccountService, [
        {
          provide: TypeORMService,
          use: typeORMService
        }
      ]);

      // WHEN
      service.$afterRoutesInit();

      // THEN
      expect(typeORMService.get).to.have.been.calledWithExactly("default");
      expect(service.repository).to.deep.eq(dbAccountRepository);
      expect(service.addressRepository).to.deep.eq(dbAddressRepository);
    });
  });
});

注意:我使用chai和sinon编写单元测试:)