无法读取未定义的Angular 2/4单元测试的属性“0”

时间:2018-01-12 06:18:15

标签: angular unit-testing typescript

我为这样的服务文件创建了一个单元测试

import { TestBed, inject } from '@angular/core/testing';
import { OrderGroupingService } from './order-grouping.service';
import { MockBackend } from '@angular/http/testing';
import { BaseRequestOptions, Jsonp, Http, HttpModule, JsonpModule, 
ResponseOptions } from '@angular/http';
import { CommonError } from 'app/services/helper/commonerror';
import { tick } from '@angular/core/testing';
import { fakeAsync } from '@angular/core/testing';
import { orderGrouping } from 'app/entity/orderGrouping';

fdescribe('OrderGroupingService', () => {
let service: OrderGroupingService;
let backend: MockBackend;
let orderGrouping: orderGrouping

beforeEach(() => {
TestBed.configureTestingModule({
  providers: [OrderGroupingService, MockBackend, BaseRequestOptions, CommonError,
    {
      provide: Jsonp,
      useFactory: (backend, options) => new Jsonp(backend, options),
      deps: [MockBackend, BaseRequestOptions]
    },
    {
      provide: Http,
      useFactory: (backend, options) => new Http(backend, options),
      deps: [MockBackend, BaseRequestOptions]
    }],
  imports: [HttpModule, JsonpModule]
});
// Get the MockBackend
backend = TestBed.get(MockBackend);
// Returns a service with the MockBackend so we can test with dummy responses
service = TestBed.get(OrderGroupingService);
});

it('Should Be Created Order Grouping Service', 
inject([OrderGroupingService], (service: OrderGroupingService) => {
expect(service).toBeTruthy();
}));

it('Add Order Groups And Returns Success Message', fakeAsync(() => {

let response = {
  "results": [
    {
      "message": "Saved successfully."
    }]
};

this.orderGrouping = {
  partyRoleId: 313,
  description: "sampleName"
}

// When the request subscribes for results on a connection, return a fake response
backend.connections.subscribe(connection => {
  connection.mockRespond(new Response(<ResponseOptions>{
    body: JSON.stringify(response)
  }));
});

tick();

// Perform a request and make sure we get the response we expect
service.addOrderGroup(this.orderGrouping).subscribe(res => {
  debugger;
  expect(res.results[0].message).toBe("Saved successfully.");
}, );

}));

 });

这是我的第一个测试用例。但第二个总是失败,它说'无法读取未定义'的属性'0'。我的第二个测试用例是“添加订单组并返回成功消息”。有什么问题吗?数据库工作正常。

0 个答案:

没有答案