Jasmine中的组件实例化失败

时间:2019-06-06 21:03:47

标签: angular jasmine

在将项目升级到Angular 7后,我正在修复一些单元测试,并且在测试中遇到了一个问题,该问题应验证组件是否已真正创建。

这是测试:

it('should build without a problem', () => {
expect(suite.appComponent).toBeTruthy();

});

它失败并显示以下错误:

  

错误:预期未定义是真实的。       在       在UserContext上。 (http://localhost:9876/_karma_webpack_/webpack:/src/app/app.component.spec.ts:93:32)       在ZoneDelegate ../ node_modules / zone.js / dist / zone.js.ZoneDelegate.invoke(http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:391:1)       在ProxyZoneSpec.push ../ node_modules / zone.js / dist / zone-testing.js.ProxyZoneSpec.onInvoke(http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:308:1

这是设置并一起进行测试:

describe('App component', () => {
let suite: TestSuite;
beforeAll(() => { suite = <TestSuite>{}; });
afterAll(() => { suite = null; });

beforeAll(() => {
suite = <TestSuite>{};
});

beforeEach((done) => {
TestBed
  .configureTestingModule({
    declarations: [
      TestComponent,
      AppComponent,
      MockComponent

    ],
    providers: [
      { provide: Router, useClass: MockRouter }

    ]
    ,
    schemas: [ NO_ERRORS_SCHEMA ]
  })
  .compileComponents().then(() => {
    suite.fixture = TestBed.createComponent(TestComponent);
    suite.appComponent = 
suite.fixture.debugElement.children[0].componentInstance;
    suite.localStorageService = 
suite.fixture.debugElement.children[0].injector.get(
      MyStorageService
    );
    suite.loginService = 
suite.fixture.debugElement.children[0].injector.get(
      MyLoginService
    );
    suite.authenticationService = 
suite.fixture.debugElement.children[0].injector.get(
      MyauthService
    );
    done();
 });
});

  it('should build without a problem', () => {
  expect(suite.appComponent).toBeTruthy();
});

最感谢您的帮助。

0 个答案:

没有答案