【问题标题】:Unit test for a subscribe function订阅功能的单元测试
【发布时间】:2021-05-12 17:07:29
【问题描述】:

我想测试我是否收到了一组人。我需要测试我是否总是得到相同类型的响应,因此如果从后端更改对象,我可以检测到它。

我的界面:

export interface People {
   peopleDtos: [{
      id: number,
      name: String,
      surname: String
      from: String,
      tel: String
   }],
   page: number,
   size: number;
   total: number;
   order: String;
}

我的组件:

export class PeopleComponent implements OnInit {
  private people: People[];

  constructor(private peopleService: peopleService) {
  }

  ngOnInit() {
    this.fetchPeople();
  }

  fetchPeople(): void {
    this.peopleService.getPeople().subscribe(response => {
      this.people = response;
    });
  }
}

我的规范文件:

import 'zone.js/dist/zone-testing';
import { async, TestBed, ComponentFixture, inject } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { PeopleService } from "../../services/people.service";
import {of} from 'rxjs/observable/of';

describe("PeopleComponent", () => {
  let component: DocumentoAudienciaComponent;
  let fixture: ComponentFixture<PeopleComponent >;
  let peopleService: PeopleService;

  beforeEach(async(() => {
    TestBed.resetTestEnvironment();
    TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
    TestBed.configureTestingModule({
      declarations: [
        PeopleComponent
      ],
      providers: [
        PeopleService
      ],
      imports: [HttpModule]
    }).compileComponents();
  }));

  beforeEach(inject([UserService], s => {
    peopleService = s;
    fixture = TestBed.createComponent(HomeComponent);
    homeComponent = fixture.componentInstance;
    element = fixture.nativeElement;
  }));

it('Should fetch people and return list of people', async(() => {
    let response: People;
    spyOn(peopleService, 'getPeople').and.returnValue(of(response));
    component.fetchPeople();
    fixture.detectChanges();
    expect(component.fetchPeople).toEqual(response);
  }));

测试不成功,我得到这个错误:

失败:无法读取未定义的属性“顺序”

【问题讨论】:

    标签: angular karma-jasmine


    【解决方案1】:

    这里你没有在线声明模拟 - let response: People; 它只是定义了模型而不是确切的模拟值:

    试试:

       it('Should fetch people and return list of people', async(() => {
            let response: People = {
                  peopleDtos: [{
                    id: 2,
                    name: 'name',
                    surname: 'surname',
                    from: 'from',
                    tel: 'tel'
                 }],
                 page: 20,
                 size: 10,
                 total: 200,
                 order: 'someOrder'
               };
                spyOn(peopleService, 'getPeople').and.returnValue(of(response));
                component.fetchPeople();
                fixture.detectChanges();
                expect(component.fetchPeople).toEqual(response);
              }));
    

    【讨论】:

    • 您可能希望根据您的测试场景调整响应对象。我为演示目的提供了一些模拟值
    猜你喜欢
    • 2020-03-09
    • 2018-02-01
    • 1970-01-01
    • 2021-11-10
    • 2017-02-26
    • 2018-11-03
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    相关资源
    最近更新 更多