订阅功能的单元测试
Unit test for a subscribe function
我想测试一下我是否收到了很多人。我需要测试我总是得到相同类型的响应,所以如果从后端更改对象我可以检测到它。
我的界面:
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);
}));
测试失败,我得到这个错误:
Failed: Cannot read property 'order' of undefined
这里你没有在线声明模拟 - 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);
}));
我想测试一下我是否收到了很多人。我需要测试我总是得到相同类型的响应,所以如果从后端更改对象我可以检测到它。
我的界面:
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);
}));
测试失败,我得到这个错误:
Failed: Cannot read property 'order' of undefined
这里你没有在线声明模拟 - 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);
}));