无法读取未定义的属性取消订阅:Angular2测试

时间:2016-10-28 17:15:45

标签: angular angular2-testing

我正在尝试测试一个具有ngOnDestroy()方法的组件,该方法具有所有unsubscribe()方法调用。但是,在测试时,当我运行我的测试文件(spec文件)时,它给出了错误说:

cannot read property 'unsubscribe' of undefined

我在测试文件中完成了以下导入:

import { Subscription }   from 'rxjs/Subscription';

所以我认为这应该足以获得Subscription中的所有方法,但仍然存在错误。此外,我尝试添加'订阅'我的'进口','声明'和'提供商'测试文件的列表,仍然存在错误。

以下是代码段:

//component
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription }   from 'rxjs/Subscription';
import {
 NinjaService } from '../../../../ninja.service';


 @Component({
 selector: 'ninja-files',
 templateUrl: './ninja-files.component.html',
 styleUrls: ['./ninja-files.component.css']
})
 export class ninjaFilesComponent implements OnInit, OnDestroy {

 showNinjaFiles: boolean = true;

 addFileSubscription: Subscription;

 constructor(private ninjaService: NinjaService) {
 ........
}

 ngOnInit() {
  this.addFileSubscription = this.NinjaService.AddedFile$
  .subscribe((fileFormat: FileFormat) => {
  console.log('File being added: ' + fileFormat.fileName); }

  ngOnDestroy() {
  this.addFileSubscription.unsubscribe();
}
}

然后我有这个组件的测试文件,如下所示:

import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription }   from 'rxjs/Subscription';
import {
 NinjaService } from '../../../../ninja.service';
import { TestBed, async, fakeAsync ,ComponentFixture, } from '@angular/core/testing';
import { DebugElement }    from '@angular/core';
import { By }              from '@angular/platform-browser';
import {} from 'jasmine';

describe('Component: NinjaFiles', () => {

  let fixture: ComponentFixture<ninjaFilesComponent>;
  let component: ninjaFilesComponent;
  let ninjaServiceStub;

beforeEach( async(() => {
 //stub NinjaService
 let ninjaServiceStub= {};

 fixture = TestBed.configureTestingModule({
 declarations: [ ninjaFilesComponent],
}).createComponent(ninjaFilesComponent);
 component = fixture.componentInstance;

}));

it('Page Title', async(() => {
 let pageTitle = fixture.debugElement.query(By.css('.page-title'));
 expect(pageTitle).toBeTruthy();
}));

 it('Counting the files', () => {
  let fileCount= fixture.debugElement.query(By.css('.file-count'));
  expect(fileCount).toBeTruthy();
});

当我运行上面的测试代码时,它会给我一个错误说&#34;无法读取属性&#39;取消订阅&#39;未定义&#34; (这意味着它无法定义订阅对象&#39; addFileSubscription&#39;我已在组件类中定义。

有人可以建议解决方法吗?

2 个答案:

答案 0 :(得分:8)

import { Subscription } from 'rxjs/Subscription';

所有这些意味着您可以从Subscription类文件访问ninjaFilesComponent类。

问题是addFileSubscription永远不会被初始化。要调用ngOnInit,您需要调用

fixture.detectChanges();

除此之外,我看到的其他问题是:

  1. 您永远不会将存根添加到提供者

    TestBed.configureTestingModule({
      providers: [
        { provide: NinjaService, useValue: ninjaServiceStub }
      ]
    })
    
  2. 您需要在存根中实际实现某些内容

    let ninjaServiceStub = {
      AddedFile$: Observable.of(new FileFormat(...))
    }
    
  3. 您没有正确使用组件中的服务

    this.NinjaService.AddedFile$
    // should be
    this.ninjaService.AddedFile$
    // lowercase
    

答案 1 :(得分:1)

在我的测试中添加fixture.detectChanges();解决了我在使用带有Material Design的Angular 2 CLI运行测试时遇到的这个问题。

感谢您的解决方法!