Jasmine 服务的单元测试没有 return 数据
Unit test for service with Jasmine does not return data
伙计们!我是测试人员的新手并且遇到了这个问题。我正在尝试为我的服务编写单元测试,它从服务器获取数据。经典案例:
import {TestBed} from '@angular/core/testing';
import {ShiftsService} from "./shifts.service";
import {Shift} from "../shift/shift";
describe('ShiftService - testing HTTP request method getShifts()', () => {
let httpTestingController: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [ShiftsService]
});
});
it('can test HttpClient.get', () => {
let shifts = new Array<Shift>();
let shiftsService;
let calendarMonth = new Date().getMonth()+2;
let calendarYear = new Date().getFullYear();
shiftsService = TestBed.inject(ShiftsService);
httpTestingController = TestBed.inject(HttpTestingController);
shiftsService.getShifts(calendarYear, calendarMonth).subscribe(response => {expect(response).toBe(response.length);
console.log(response);
});
let apiRequest:string = '/api/shifts?year='.concat(calendarYear.toString()).concat('&month=').concat(calendarMonth.toString());
const req = httpTestingController.expectOne(apiRequest);
console.log(apiRequest);
expect(req.request.method).toBe('GET');
req.flush(shifts);
});
afterEach(() => httpTestingController.verify());
});
我在服务文件中的方法如下所示:
getShifts (year: number, month: number): Observable<Shift[]> {
let params = new HttpParams();
params = params.append('year', year.toString());
params = params.append('month', month.toString());
return this.http.get<Shift[]>(this.shiftsUrl, { params: params })
.pipe(tap((shifts) => shifts),
catchError((err) => this.handleError(err)))
}
我收到错误:错误:预期 [ ] 为 0。 当我打印出 response 变量时,我得到了它是空的!但我确信这种方法可以正常工作!它在我的应用程序中运行良好!你能帮我解决这个问题吗?如何更正我的测试方法来测试服务?
在最后,您正在做 req.flush(shifts)
和 shifts = new Array<Shift>();
,这实际上是 []
。刷新是您希望 HTTP get 请求响应的内容,在本例中它是一个空数组。
在订阅中,您断言 response
([]
) 等于 response.length
即 0
.
试试这个:
it('can test HttpClient.get', (done) => { // add done callback to be able to call it in the subscribe
let shifts = new Array<Shift>();
let shiftsService;
let calendarMonth = new Date().getMonth()+2;
let calendarYear = new Date().getFullYear();
shiftsService = TestBed.inject(ShiftsService);
httpTestingController = TestBed.inject(HttpTestingController);
shiftsService.getShifts(calendarYear, calendarMonth).subscribe(response => {
// we have to use toEqual because toBe does a deep assertion
// but the array to compare to is in a different location in memory so
// toBe would fail
expect(response).toEqual([]);
console.log(response);
// call done to tell the unit test you are done with this test
done();
});
let apiRequest:string = '/api/shifts?year='.concat(calendarYear.toString()).concat('&month=').concat(calendarMonth.toString());
const req = httpTestingController.expectOne(apiRequest);
console.log(apiRequest);
expect(req.request.method).toBe('GET');
shifts.push(/* push a shift here */) // change the shifts array to what you want the server to respond with
req.flush(shifts);
});
伙计们!我是测试人员的新手并且遇到了这个问题。我正在尝试为我的服务编写单元测试,它从服务器获取数据。经典案例:
import {TestBed} from '@angular/core/testing';
import {ShiftsService} from "./shifts.service";
import {Shift} from "../shift/shift";
describe('ShiftService - testing HTTP request method getShifts()', () => {
let httpTestingController: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [ShiftsService]
});
});
it('can test HttpClient.get', () => {
let shifts = new Array<Shift>();
let shiftsService;
let calendarMonth = new Date().getMonth()+2;
let calendarYear = new Date().getFullYear();
shiftsService = TestBed.inject(ShiftsService);
httpTestingController = TestBed.inject(HttpTestingController);
shiftsService.getShifts(calendarYear, calendarMonth).subscribe(response => {expect(response).toBe(response.length);
console.log(response);
});
let apiRequest:string = '/api/shifts?year='.concat(calendarYear.toString()).concat('&month=').concat(calendarMonth.toString());
const req = httpTestingController.expectOne(apiRequest);
console.log(apiRequest);
expect(req.request.method).toBe('GET');
req.flush(shifts);
});
afterEach(() => httpTestingController.verify());
});
我在服务文件中的方法如下所示:
getShifts (year: number, month: number): Observable<Shift[]> {
let params = new HttpParams();
params = params.append('year', year.toString());
params = params.append('month', month.toString());
return this.http.get<Shift[]>(this.shiftsUrl, { params: params })
.pipe(tap((shifts) => shifts),
catchError((err) => this.handleError(err)))
}
我收到错误:错误:预期 [ ] 为 0。 当我打印出 response 变量时,我得到了它是空的!但我确信这种方法可以正常工作!它在我的应用程序中运行良好!你能帮我解决这个问题吗?如何更正我的测试方法来测试服务?
在最后,您正在做 req.flush(shifts)
和 shifts = new Array<Shift>();
,这实际上是 []
。刷新是您希望 HTTP get 请求响应的内容,在本例中它是一个空数组。
在订阅中,您断言 response
([]
) 等于 response.length
即 0
.
试试这个:
it('can test HttpClient.get', (done) => { // add done callback to be able to call it in the subscribe
let shifts = new Array<Shift>();
let shiftsService;
let calendarMonth = new Date().getMonth()+2;
let calendarYear = new Date().getFullYear();
shiftsService = TestBed.inject(ShiftsService);
httpTestingController = TestBed.inject(HttpTestingController);
shiftsService.getShifts(calendarYear, calendarMonth).subscribe(response => {
// we have to use toEqual because toBe does a deep assertion
// but the array to compare to is in a different location in memory so
// toBe would fail
expect(response).toEqual([]);
console.log(response);
// call done to tell the unit test you are done with this test
done();
});
let apiRequest:string = '/api/shifts?year='.concat(calendarYear.toString()).concat('&month=').concat(calendarMonth.toString());
const req = httpTestingController.expectOne(apiRequest);
console.log(apiRequest);
expect(req.request.method).toBe('GET');
shifts.push(/* push a shift here */) // change the shifts array to what you want the server to respond with
req.flush(shifts);
});