gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:38:17 25 4
gpt4 key购买 nike

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

例如服务方法:

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

关于angular - 如何为 ng2 单元测试模拟 ng2-restangular?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41936716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com