【问题标题】:Angular 4 unit test get error - Failed: Cannot read property 'map' of undefinedAngular 4 单元测试出错 - 失败:无法读取未定义的属性“地图”
【发布时间】:2018-02-15 15:43:15
【问题描述】:

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

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

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

看看我的代码:

我的规格

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

【问题讨论】:

  • 你在嘲笑http服务吗?还是在测试中实际向您的服务器发送请求?

标签: javascript angular unit-testing


【解决方案1】:

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

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

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

添加这一行:

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

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

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

在此处了解有关此方法的更多信息:https://jasmine.github.io/api/2.7/Spy_and.html

【讨论】:

  • 我现在收到此错误Failed: response.json is not a function
  • 您需要将json 属性添加到您的测试响应中。 json 属性应该是一个函数,它返回一些带有您想要的数据的 JSON 对象。我用一个看起来如何的例子更新了我的答案。您的原始错误已解决! :D
  • 抱歉,我到底要装什么 return // object with properties you want to test 我试图放一些东西,但都出错了
  • 如果没有看到您尝试了什么以及遇到了什么错误,我无法告诉您。您在此问题中询问的原始错误已解决。新错误需要考虑的一些事情:它在实际程序中返回什么(将其记录到控制台并检查)?你想测试什么属性?由于您的原始问题已解决,请考虑接受和/或支持此答案,以便其他人知道它已解决。如果您仍然遇到新错误,请打开一个新问题。欢迎来到 Stackoverflow,祝你好运:)
  • 其实我的问题是如何在这个测试中取得成功 =/ ,但是谢谢你的帮助,我会继续尝试上面的提示。
猜你喜欢
  • 1970-01-01
  • 2018-07-27
  • 2018-02-14
  • 2022-06-22
  • 2017-04-04
  • 1970-01-01
  • 2023-01-09
  • 2018-12-23
  • 2021-01-22
相关资源
最近更新 更多