Yuriy K.
Yuriy K.

Reputation: 241

How to mock ng2-restangular for ng2 unit-tests?

I'm trying to find the correct way of mocking ng2-restangular for testing my ng2 services.

For example for the service method:

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

Upvotes: 3

Views: 300

Answers (1)

Tonyo
Tonyo

Reputation: 36

Link: 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]
  },
];

Upvotes: 2

Related Questions