如何为ng2单元测试模拟ng2-restangular?

时间:2017-01-30 13:07:55

标签: angular

我正在尝试找到模拟ng2-restangular的正确方法来测试我的ng2服务。

例如,对于服务方法:

get(id) {
  return this.restangular.all('notes').get(id);
}

1 个答案:

答案 0 :(得分:2)

链接:https://github.com/2muchcoffeecom/ng2-restangular/blob/master/demo/mock-data/mock-providers.ts

import {BaseRequestOptions, Http} from "@angular/http";
import {MockBackend} from "@angular/http/testing";
import {RestangularHttp} from "../../src/ng2-restangular-http";

export const MockProviders = [
  BaseRequestOptions,
  MockBackend,
  {
    provide: RestangularHttp,
    useFactory: (http: Http) => {
      return new RestangularHttp(http);
    },
    deps: [Http]
  },
  {
    provide: Http,
    useFactory: (backendInstance: MockBackend, defaultOptions: BaseRequestOptions) => {
      return new Http(backendInstance, defaultOptions);
    },
    deps: [MockBackend, BaseRequestOptions]
  },
];