0

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

在此处输入图像描述

4

1 回答 1

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();
    });

  });
于 2017-09-28T05:24:29.120 回答