angular - Jasmine 测试 TypeORM - 错误: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
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'
,sqlite
则使用库在正在运行的实例中创建和使用sqlite3
。
如果您想使用 external sqlite3
,请查看此页面。
推荐阅读
- android - Inode 和已删除的文件条目
- powerbuilder - PowerBuilder - 重复值抑制列表不适用于某些数据窗口列
- javascript - vue js在元素上应用动画功能
- php - wordpress 4.9.5:正在上传图片,但无法在媒体和功能下显示
- r - 将定量变量分配给 R 中数据框中的数据点
- python-unittest - Eric IDE:在 IDE 中运行单元测试
- vue.js - 带有 this.$route.params.slug 的 API 响应
- vba - VBA 在满足条件时复制单元格
- javascript - Javascript :: XML 中的网格/列表视图
- android - 在即时应用上启用推送通知