gpt4 book ai didi

angular - TypeError : jit__object_Object_38 is not a constructor, NgRedux, Angular 2

转载 作者:行者123 更新时间:2023-12-02 10:51:19 26 4
gpt4 key购买 nike

请原谅我只是在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

enter image description here

最佳答案

对我来说,问题是我试图使用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();
});

});

关于angular - TypeError : jit__object_Object_38 is not a constructor, NgRedux, Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46461324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com