首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角业力-茉莉花不能读取未定义的属性“获取”。

问角业力-茉莉花不能读取未定义的属性“获取”。
EN

Stack Overflow用户
提问于 2017-08-24 12:50:41
回答 1查看 5.6K关注 0票数 2

example.service.ts

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, URLSearchParams, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
import { environment as ENV } from '../../environments/environment';
import { SharedService } from './shared.service';

video(id): Observable<any> {
let sessionId = this.sharedService.sessionId();

return this.http.get(ENV.api_url + '&videoId=' + id)
      .map(this.extractData)
      .catch(this.handleError);

}

example.service.spec.ts

代码语言:javascript
复制
import { async, ComponentFixture, TestBed } from 

'@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { Http, HttpModule } from '@angular/http';
import { MockBackend } from '@angular/http/testing';


describe('VideosComponent', () => {
  let component: VideosComponent;
  let fixture: ComponentFixture<VideosComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule
      ],
      declarations: [ VideosComponent ],
      providers: [
        {
          provide: Http,
          deps: [MockBackend]
        },
      ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(VideosComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

当我试图运行测试时,我得到了TypeError:无法读取未定义的属性'get‘。我对单元测试非常陌生,但是我在这里发布了返回'get‘的单元测试文件和服务的代码。

EN

回答 1

Stack Overflow用户

发布于 2017-08-24 13:07:41

首先,你的服务不是服务。我真的推荐你读https://angular.io/tutorial/toh-pt6#heroes-and-http。

在这里您可以看到您需要创建一个constructor(private http: Http) { },您的http现在是未定义的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45862142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档