请原谅我只是尝试Angular 2中的单元测试中的事情。执行npm run test
时会出现这种奇怪的错误。可能是一个小错误,但无法弄清楚这个奇怪的错误:
import { TestBed, async, inject } from '@angular/core/testing';
import { TestModule } from '../../test/test.module';
import { DevExtremeModule } from 'devextreme-angular';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpModule, XHRBackend, RequestOptions } from '@angular/http';
import { Templates } from './templates.component';
import { CommandButton } from '../../common/tool/command-button.component';
import { NgReduxModule, NgRedux } from '@angular-redux/store/lib';
import { MockBackend } from "@angular/http/testing/mock_backend";
import { HttpService } from "../../services/http.service";
import { AppState } from '../../redux/index';
// Mock out the NgRedux class with just enough to test what we want.
class MockRedux extends NgRedux<AppState> {
constructor(private state: AppState) {
super(null);
}
dispatch = () => undefined;
getState = () => this.state;
}
describe('Component : Templates', () => {
let fixture;
let mockRedux: NgRedux<AppState>;
let mockState : AppState = {
session : { token: '', user: { firstName : 'Test User' }, isError: null }
};
beforeEach(done => {
mockRedux = new MockRedux(mockState);
const configure = (testBed: TestBed) => {
testBed.configureTestingModule({
imports: [ TestModule, DevExtremeModule,
RouterTestingModule, HttpModule ],
declarations : [ Templates ],
providers: [
{ provide: XHRBackend, useClass: MockBackend },
{
provide: HttpService,
useFactory: (backend: XHRBackend, options: RequestOptions, redux: NgRedux<AppState> ) => {
return new HttpService(backend, options, mockRedux );
},
deps: [XHRBackend, RequestOptions]
},
{ provide: NgRedux, useClass : mockRedux }
]
});
};
TestModule.configureTests(configure).then(testBed => {
fixture = testBed.createComponent(RegTemplates);
fixture.detectChanges();
done();
});
});
it('should instantiate Templates Component', async(inject([], () => {
fixture.whenStable().then(() => {
fixture.autoDetectChanges();
expect(fixture.componentInstance).toBeDefined();
});
})));
// To do
});
错误说:
TypeError: jit__object_Object_38 is not a constructor
答案 0 :(得分:2)
对我而言,问题是我试图使用useClass
代替useValue
,可能是一个大错,但是很难学会......;)
{ provide: NgRedux, useValue : mockRedux }
是正确的用法,而不是{ provide: NgRedux, useClass : mockRedux }
beforeEach(done => {
mockRedux = new MockRedux(mockState);
const configure = (testBed: TestBed) => {
testBed.configureTestingModule({
imports: [ TestModule, DevExtremeModule,
RouterTestingModule, HttpModule ],
declarations : [ RegTemplates, CommandButton, RegHeader,
RegStructureImage ],
providers: [
{ provide: XHRBackend, useClass: MockBackend },
{
provide: HttpService,
useFactory: (backend: XHRBackend, options: RequestOptions, redux: NgRedux<AppState> ) => {
return new HttpService(backend, options, mockRedux );
},
deps: [XHRBackend, RequestOptions]
},
{ provide: NgRedux, useValue : mockRedux }
]
});
};
TestModule.configureTests(configure).then(testBed => {
fixture = testBed.createComponent(RegTemplates);
fixture.detectChanges();
done();
});
});