我看到https://github.com/2muchcoffeecom/ngx-restangular/blob/master/demo/mock-data/mock-providers.ts作为对之前提出的类似问题的回答,但我无法理解如何使用它。有人可以解释一下吗?
我的问题 -
我有一种服务方法 -
restoreProject(project) {
this.restangular.one('projects', project.id).get();
}
我如何模拟restangular呢?
直到现在我
beforeEach(() => {
this.injector = Injector.create([
{provide: ConnectionBackend, useClass: MockBackend, deps: []},
{provide: RequestOptions, useClass: BaseRequestOptions, deps: []},
{provide: Restangular, useClass: Restangular, deps:[]},
{provide: ProjectService, useClass: ProjectService, deps:[Restangular]}
]);
this.service = this.injector.get(ProjectService);
this.backend = this.injector.get(ConnectionBackend) as MockBackend;
this.backend.connections.subscribe((connection: any) => this.connection = connection);
});
it('should query restore project', () => {
this.service.restoreProject({id: 2});
expect(this.connection.request.url).toMatch('projects/2');
}));
但这会产生以下错误
TypeError: Cannot read property 'createRequest' of undefined
at Object.resource.(anonymous function) [as get] (http://0.0.0.0:9876/_karma_webpack_/webpack:/home/app/node_modules/ngx-restangular/dist/esm/src/ngx-restangular-config.factory.js:475:1)
at Object.elemFunction (http://0.0.0.0:9876/_karma_webpack_/webpack:/home/app/node_modules/ngx-restangular/dist/esm/src/ngx-restangular.js:396:1)
at wrapper (http://0.0.0.0:9876/_karma_webpack_/webpack:/home/app/node_modules/lodash/lodash.js:4968:1)
at Object.getFunction (http://0.0.0.0:9876/_karma_webpack_/webpack:/home/app/node_modules/ngx-restangular/dist/esm/src/ngx-restangular.js:405:1)
at Object.wrapper [as get] (http://0.0.0.0:9876/_karma_webpack_/webpack:/home/app/node_modules/lodash/lodash.js:4968:1)
at CommunicatorProjectService.restoreProject (http://0.0.0.0:9876/_karma_webpack_/webpack:/home/app/src/app/budget-and-time-estimator/shared/communicator-project/communicator-project.service.ts:21:54)
at Object.<anonymous> (http://0.0.0.0:9876/_karma_webpack_/webpack:/home/app/src/app/budget-and-time-estimator/shared/communicator-project/communicator-project.service.spec.ts:35:17)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (http://0.0.0.0:9876/_karma_webpack_/webpack:/home/app/node_modules/zone.js/dist/zone.js:392:1)
at ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (http://0.0.0.0:9876/_karma_webpack_/webpack:/home/app/node_modules/zone.js/dist/proxy.js:79:1)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (http://0.0.0.0:9876/_karma_webpack_/webpack:/home/app/node_modules/zone.js/dist/zone.js:391:1)