簡體   English   中英

Jasmine 測試 TypeORM - 錯誤:DriverPackageNotInstalledError:未找到安裝的 SQLite 包

[英]Jasmine Testing TypeORM - Error: DriverPackageNotInstalledError: SQLite package has not been found installed

我正在嘗試對使用 TypeORM 連接更新 sqlite 數據庫的服務進行單元測試。

看起來沒有固定的方法來對 TypeORM 進行單元測試,所以我試圖找到任何可行的方法。 根據以下內容,我決定嘗試使用內存數據庫進行測試

https://github.com/typeorm/typeorm/issues/1267#issuecomment-483775861

那里的代碼使用 Jest,所以我試圖轉換為 Jasmine,這是我的測試環境。

作為原理證明,如果我在database-test.spec.ts執行以下database-test.spec.ts

import { createConnection, Entity, getRepository } from "typeorm";
import { PrimaryGeneratedColumn, Column } from "typeorm";
import { fakeAsync, async } from "@angular/core/testing";

@Entity()
export class MyEntity {
  @PrimaryGeneratedColumn()
  id?: number;

  @Column()
  name?: string;
}

describe("Database test", () => {
  it("store Joe and fetch it", fakeAsync(() => {
    createConnection({
      name: "testing",
      type: "sqlite",
      database: ":memory:",
      dropSchema: true,
      entities: [MyEntity],
      synchronize: true,
      logging: false,
    }).then(conn => {
      conn.getRepository(MyEntity).insert({
        name: "Joe",
      });
      const joe = getRepository(MyEntity).find({
        where: {
          id: 1,
        },
      });

      expect(joe[0].name).toBe("Joe");
      conn.close();
    });
  }));
});

無論我做什么我都會出錯

Failed: Uncaught (in promise): DriverPackageNotInstalledError: SQLite package has not been found installed. Try to install it: npm install sqlite3 --save
DriverPackageNotInstalledError: SQLite package has not been found installed. Try to install it: npm install sqlite3 --save
    at new DriverPackageNotInstalledError (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/error/DriverPackageNotInstalledError.js:8:1)
    at SqliteDriver.loadDependencies (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/driver/sqlite/SqliteDriver.js:118:1)
    at new SqliteDriver (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/driver/sqlite/SqliteDriver.js:24:1)
    at DriverFactory.create (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/driver/DriverFactory.js:35:1)
    at new Connection (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/connection/Connection.js:50:40)
    at ConnectionManager.create (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/connection/ConnectionManager.js:54:1)
    at Module.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/index.js:171:1)
    at step (http://localhost:9876/_karma_webpack_/webpack:/node_modules/tslib/tslib.es6.js:99:1)
    at Object.next (http://localhost:9876/_karma_webpack_/webpack:/node_modules/tslib/tslib.es6.js:80:45)
    at http://localhost:9876/_karma_webpack_/webpack:/node_modules/tslib/tslib.es6.js:73:1

SQLite 已安裝並在測試之外工作。 為什么我在測試過程中會收到此錯誤?

其他信息:我正在使用入門套件: https : //github.com/CubikNeRubik/angular-electron-typeorm-starter

安裝sqlite3

如果 typeorm 選項指定為database:':memory' ,則使用sqlite3庫在運行實例中創建和使用sqlite

如果您想使用外部sqlite3 ,請查看此頁面。

https://github.com/mapbox/node-sqlite3#source-install

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM