Angular 4 单元测试出现错误 - 失败:无法读取未定义的属性“地图" [英] Angular 4 unit test get error - Failed: Cannot read property 'map' of undefined

查看:21
本文介绍了Angular 4 单元测试出现错误 - 失败:无法读取未定义的属性“地图"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Angular 4 单元测试中取得成功,但我收到了不同的错误,我收到了错误 Failed: Cannot read property 'map' of undefined.>

我仅在服务文件上使用 .map() 但我不知道为什么会显示此错误

我想知道我如何才能通过此测试获得成功,如果我走对了路?

看看我的代码:

我的规格

import { TestBed, async, inject } from '@angular/core/testing';从'@angular/http'导入{HttpModule};从 'rxjs/observable/of' 导入 { of };从'../../../models/fileX'导入{文件X};从'../../../services/fileX.service'导入{fileXService};import { fileYfileXComponent } from './fileY-fileX.component';从'../../../services/data.service'导入{数据服务};从@angular/core/testing"导入{ComponentFixture};描述('fileYfileXComponent',()=> {让 fileXService;让我的组件;让夹具:任何;让元素;让 mockHttp: httpClient;beforeEach(async(() => {mockHttp = jasmine.createSpyObj('http', ['get']);TestBed.configureTestingModule({声明:[fileYfileXComponent],供应商: [文件X服务,数据服务,{ 提供:Http,useValue:mockHttp }],进口:[HttpModule]}).compileComponents();}));beforeEach(inject([fileXService], s => {fileXService = s;夹具 = TestBed.createComponent(fileYfileXComponent);myComponent = fixture.componentInstance;元素=fixture.nativeElement;}));它('应该得到值',异步(() => {常量响应:fileX[] = [];spyOn(fileXService, 'method1').and.returnValue(of(response));myComponent.searchdatafileX();夹具.detectChanges();期望(myComponent.datafileX).toEqual(响应);}));});

解决方案

目前您正在正确地模拟 HubWrapperComponent,这样它就不会在您每次调用 获取.为了让它工作,您需要使用 Jasmine 的 Spy 实用程序 returnValue 来告诉测试在调用 http.get 时应该返回什么.现在,这没有被指定,所以 mapundefined 上被调用.

在您的规范文件中,在该行之后:

mockHub = jasmine.createSpyObj('hubWrapperComponent', ['get']);

添加这一行:

mockHub.get.and.returnValue(//你的测试响应);

根据服务中的代码,我的假设是您期望来自 get 方法的 Observable,因此您的 returnValue 调用将如下所示:

mockHub.get.and.returnValue(Observable.of({json: () =>{return//带有要测试的属性的对象}}));

这里有关于这个方法的更多信息:https://jasmine.github.io/api/2.7/Spy_and.html.

I'm trying to get success on Angular 4 unit test, but I'm getting differents error, I'm getting the error Failed: Cannot read property 'map' of undefined.

I'm using .map() only on service file but I don't know why showing this error

I'd like to know how can I get success for this test, and if, am I on the right way?

Take a look my code:

My Spec

import { TestBed, async, inject } from '@angular/core/testing';
import { HttpModule } from '@angular/http';
import { of } from 'rxjs/observable/of';
import { fileX } from '../../../models/fileX';
import { fileXService } from '../../../services/fileX.service';
import { fileYfileXComponent } from './fileY-fileX.component';
import { dataService } from '../../../services/data.service';
import { ComponentFixture } from '@angular/core/testing';

describe('fileYfileXComponent', () => {
  let fileXService;
  let myComponent;
  let fixture: any;
  let element;
  let mockHttp: httpClient;

  beforeEach(async(() => {
      mockHttp = jasmine.createSpyObj('http', ['get']);

      TestBed.configureTestingModule({
          declarations: [fileYfileXComponent],
          providers: [
              fileXService,
              dataService,
              { provide: Http, useValue: mockHttp }
          ],
          imports: [HttpModule]
      }).compileComponents();
    }));

  beforeEach(inject([fileXService], s => {
      fileXService = s;
      fixture = TestBed.createComponent(fileYfileXComponent);
      myComponent = fixture.componentInstance;
      element = fixture.nativeElement;
    }));

  it(
    'should get values',
    async(() => {
      const response: fileX[] = [];

      spyOn(fileXService, 'method1').and.returnValue(of(response));

      myComponent.searchdatafileX();

      fixture.detectChanges();

      expect(myComponent.datafileX).toEqual(response);
    })
  );
});

解决方案

Currently you are mocking the HubWrapperComponent correctly, such that it will not make a call to your back end every time you invoke get. In order to get this working, you'll need to use Jasmine's Spy utility, returnValue to tell the test what should be returned when http.get is called. Right now, this is not specified so the map is being called on undefined.

In your spec file, after the line:

mockHub = jasmine.createSpyObj('hubWrapperComponent', ['get']);

Add this line:

mockHub.get.and.returnValue(// your test response);

Based on the code in the service, my assumption is that you are expecting an Observable from the get method, so your returnValue call will look something like this:

mockHub.get.and.returnValue(Observable.of(
    { 
      json: () => { 
        return // object with properties you want to test 
      } 
    }
  )
);

More information on this method here: https://jasmine.github.io/api/2.7/Spy_and.html.

这篇关于Angular 4 单元测试出现错误 - 失败:无法读取未定义的属性“地图"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆