使用Angular 2中的构造函数http:Http实例化服务

时间:2017-01-18 13:38:59

标签: angular service angular-services angular-http angular2-testing

我这里有这个服务类:

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import {ReflectiveInjector} from '@angular/core';
import { Observable } from 'rxjs/Rx';

import { Nota } from './nota.model';
import { HttpUtilService } from '../services/http-util.service';

import moment = require('moment');

const TIPOS_DOC : any[] = [
  {id: 1, nome: "Sem Referência"},
  ...

@Injectable()
export class NotaService {

    constructor(private http: Http, private httpUtil: HttpUtilService) { }

// methods ...
}

我想在我的spec文件中实例化这个服务,这样我就可以调用这些方法并进行测试。但是当我实例化服务时,我不知道如何正确使用http:Http。这是我的测试文件:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By }              from '@angular/platform-browser';
import { DebugElement }    from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';

import { HttpUtilService } from '../../services/http-util.service';

import { NotaService } from '../nota.service';

describe ('Tests for the service Nota response', () => {

  beforeEach(() => {
      let http_service  = new HttpUtilService();
      let http: Http;
      this.nota_service = new NotaService(http, http_service);
  });
  it ('testes ...')
  ...

});

我没有得到任何语法错误,但此测试文件不起作用。

2 个答案:

答案 0 :(得分:0)

如果您在beforeEach内检查代码,则基本上定义Http,但根本不对其进行实例化。您的NotaService将收到NULL引用。参见:

  let http: Http;
  this.nota_service = new NotaService(http, http_service);

常见的方法是提供一些假的Http服务(Mock)并将其注入您的服务。你需要告诉Angular 2:“如果有人想要Http,请给他Mocked版本。”

在测试中使用类似的东西:

import { addProviders, inject } from '@angular/core/testing';
import { Http, BaseRequestOptions } from '@angular/http';
import { MockBackend } from '@angular/http/testing';
import { NotaService} from '../nota.service';

...

beforeEach(() => {
  addProviders([
    MockBackend,
    BaseRequestOptions,
    {
      provide: Http,
      useFactory: (backendInstance: MockBackend, defaultOptions: BaseRequestOptions) => {
        return new Http(backendInstance, defaultOptions);
      },
      deps: [MockBackend, BaseRequestOptions]
    },
    NotaService
  ]);
});

查看此文章了解详细说明Testing Angular 2 HTTP Services with Jasmine  并查找介绍HTTP服务

部分

答案 1 :(得分:0)

以下是我正在进行测试的方法。

首先,在Angular 2中,您不应该尝试在前端(视图)测试中实例化服务。总是使用嘲笑。它们非常容易创建和使用。

以下是模拟文件的示例

// import the model you want to test and their dependencies
import { Post } from "../app/post/post.model";
import { UserTestMock } from "./mocks/user.test";

// create a normal class that will be the mock of your model
export class PostTestMock {

  static getMock(id: number): Post {
    const post = new Post(
      id,
      "Title of my post",
      "The body of my post ... end of body.",
      UserTestMock.getMock(1)
    );

    return post;
  }
}

在您的测试文件中,您只需要使用(导入它)您的模拟

  it("Should test something", async(() => {
    let post = PostTestMock.getMock(1);
    // just an example of some function that you want to call
    yourTest.send(post)
  }));