我正在为一个名为TaskService
. 单元测试注入一个 TaskService 实例,其构造函数继续创建到 NeDB 的数据库连接。(我知道我应该模拟这个连接,但我需要先让单元测试工作,然后才能优化它)。
任务服务.ts
import {Injectable, OnDestroy} from '@angular/core';
import {Task} from "./task.model";
import Datastore from 'nedb';
@Injectable()
export class TaskService implements OnDestroy {
private tasks: any;
constructor(private _logger: Logger) {
this._logger.log("Task Service Constructor");
this.tasks = new Datastore({filename: 'db/tasks.json'});
}
错误信息
TypeError: nedb_1.default is not a constructor
at new TaskService I:/Projects/taskmelater/src/app/tasks/shared/task.service.ts:22:18)
at _createClass I:/Projects/taskmelater/node_modules/@angular/core/@angular/core.es5.js:9529:1)
at _createProviderInstance$1 I:/Projects/taskmelater/node_modules/@angular/core/@angular/core.es5.js:9503:1)
at resolveNgModuleDep I:/Projects/taskmelater/node_modules/@angular/core/@angular/core.es5.js:9488:1)
at NgModuleRef_.webpackJsonp.../../../core/@angular/core.es5.js.NgModuleRef_.get I:/Projects/taskmelater/node_modules/@angular/core/@angular/core.es5.js:10562:1)
at TestBed.webpackJsonp.../../../core/@angular/core/testing.es5.js.TestBed.get I:/Projects/taskmelater/node_modules/@angular/core/@angular/core/testing.es5.js:819:1)
at Function.webpackJsonp.../../../core/@angular/core/testing.es5.js.TestBed.get I:/Projects/taskmelater/node_modules/@angular/core/@angular/core/testing.es5.js:656:1)
at UserContext.<anonymous> I:/Projects/taskmelater/src/app/tasks/shared/task.service.spec.ts:20:24)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke I:/Projects/taskmelater/node_modules/zone.js/dist/zone.js:392:1)
at ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke I:/Projects/taskmelater/node_modules/zone.js/dist/proxy.js:79:1)
测试运行者抱怨这this.tasks = new Datastore({filename: 'db/tasks.json'});
条线。
这是 NeDB 包的 index.js 文件。
index.js
var Datastore = require('./lib/datastore');
module.exports = Datastore;
app.module.ts
@NgModule({
declarations: [
AppComponent,
StarterComponent,
StarterHeaderComponent,
StarterLeftSideComponent,
StarterContentComponent,
StarterFooterComponent,
StarterControlSidebarComponent,
TasksComponent,
TaskComponent,
TaskFormComponent,
TaskService
],
imports: [
BrowserModule,
AppRoutingModule,
AdminModule,
AgGridModule,
RouterModule,
FormsModule
],
providers: [TaskService, Logger],
bootstrap: [AppComponent, TaskService, Logger]
})
export class AppModule {}
咨询的问题:
我无法在这个问题中应用解决方案,因为 NeDB 是一个 NPM 包,而不是我自己的类: 错误:* .default 不是构造函数