5
我試圖找到正確的方法來模擬ng2-restangular來測試我的ng2服務。如何爲ng2單元測試模擬ng2-restangular?
例如,對於服務方法:
get(id) {
return this.restangular.all('notes').get(id);
}
我試圖找到正確的方法來模擬ng2-restangular來測試我的ng2服務。如何爲ng2單元測試模擬ng2-restangular?
例如,對於服務方法:
get(id) {
return this.restangular.all('notes').get(id);
}
鏈接: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]
},
];
雖然這可以回答這個問題,考慮增加一個簡短的說明 –
謝謝Tonyo!進一步調查的好點 –