Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
角度2:结合夹具的测试http观察值_Http_Angular - Fatal编程技术网

角度2:结合夹具的测试http观察值

角度2:结合夹具的测试http观察值,http,angular,Http,Angular,组成部分: export class Component implements OnInit { private element: any; private data: any; constructor(@Inject(ElementRef) private elementRef: ElementRef, private service: Service) {} ngOnInit() { this.element = select(this.elementRef.na

组成部分:

export class Component implements OnInit {
  private element: any;
  private data: any;

  constructor(@Inject(ElementRef) private elementRef: ElementRef, private service: Service) {}

  ngOnInit() {
    this.element = select(this.elementRef.nativeElement);
    this.service.getData().subscribe((data: Data) => {
      this.data = data;
    });
  }
}
组件测试:

describe('Component', () => {
  let component;
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [Component, Service],
      imports: [TestModule]
    });

    let fixture = TestBed.createComponent(Component);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  let data = {};
  it('should get data',
     async(inject([MockBackend], (backend) => {
       backend.connections.subscribe((connection) => {
         connection.mockRespond(new Response(new ResponseOptions({status: 200, body: data})));
       });
       component.ngOnInit();
       expect(component.data).toHaveEqualContent(data);
     })));
});
无法解析可观察的
getData()
,测试将失败,并出现以下错误:

TypeError:无法读取未定义的属性“json”

当我丢失夹具并将
组件
注入测试时,测试将无法处理
ElementRef
并失败:

失败:this.querySelector不是函数

如何将http可观察性与夹具结合起来进行测试