简体   繁体   English

TypeError:undefined不是对象(评估'res [0] .id')

[英]TypeError: undefined is not an object (evaluating 'res[0].id')

I am trying to mock backend service for unit testing. 我正在尝试模拟后端服务进行单元测试。 Below is code service 以下是代码服务

getFreightById(id: number): Promise < Freight[] > {

    let searchParams = new URLSearchParams();
    searchParams.set('id', id.toString());
    return this.http.get("app/freight/getById", {
            search: searchParams
        })
        .toPromise()
        .then(this.extractData)
        .catch(this.handleError);
}

Below is my Jasmine Unit test case: 以下是我的茉莉花单元测试用例:

describe('FreightService', () => {
  beforeEachProviders(() => [
      BaseRequestOptions,
      MockBackend,
      FreightService,
      provide(Http, {
        useFactory: (backend: ConnectionBackend,
                     defaultOptions: BaseRequestOptions) => {
        return new Http(backend, defaultOptions);
      }, deps: [MockBackend, BaseRequestOptions]}),

  ]);

  describe('getFreightById', () => {
    it('retrieves using the Freight ID',
      inject([FreightService, MockBackend], fakeAsync((freightService, backend) => {
        var res;
        expectURL(backend, 'app/freight/getById?id=4');
        freightService.getFreightById(4).then((_res) => {
          res = _res;
        })
        tick();
        expect(res[0].id).toBe(4);
      }))
    );
  });

    // sets up an expectation that the correct URL will being requested
  function expectURL(backend: MockBackend, url: string) {
    backend.connections.subscribe(c => {
      expect(c.request.url).toBe(url);
      let response = new ResponseOptions({
                     body: '[{"id": "4"}]' });
      c.mockRespond(new Response(response));
    });
  }
})

I am getting error "TypeError: undefined is not an object (evaluating 'res[0].id')". 我收到错误“ TypeError:未定义不是对象(正在评估'res [0] .id')”。 Any idea what is wrong here?? 知道这里有什么问题吗? Please help!! 请帮忙!!

找到的解决方案:我的回复正文格式不正确。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError: undefined is not an object(评估'ref.onSnapshot') - TypeError: undefined is not an object (evaluating 'ref.onSnapshot') TypeError:未定义不是对象(评估“ parentOutlet.peekState”) - TypeError: undefined is not an object (evaluating 'parentOutlet.peekState') 为什么会出现TypeError:undefined不是Angular中的对象(求值”)? - Why I get TypeError: undefined is not an object (evaluating '') in Angular? 例外:TypeError:未定义不是Ionic 2中的对象(评估“ pipe.constructor”)吗? - EXCEPTION: TypeError: undefined is not an object (evaluating 'pipe.constructor') in Ionic 2? 未定义不是对象(评估)-角度 - Undefined is not an object(evaluating) - angular TypeError:未定义不是构造函数(正在评估“ new viewClass()”) - TypeError: undefined is not a constructor (evaluating 'new viewClass()') 未定义不是对象(评估“ type.parameters”) - Undefined is not an object (evaluating 'type.parameters') undefined 不是对象(评估 &#39;this.http.get&#39;) - undefined is not an object (evaluating 'this.http.get') TypeError: undefined is not a constructor (评估 &#39;this.service.getDat().subscribe&#39;) - TypeError: undefined is not a constructor (evaluating 'this.service.getDat().subscribe') 错误TypeError:无法读取Object.eval上未定义的属性&#39;_id&#39;[作为updateDirectives] - ERROR TypeError: Cannot read property '_id' of undefined at Object.eval [as updateDirectives]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM