gpt4 book ai didi

Angular Testing karma - 错误 : Can't resolve all parameters for RequestOptions: (?)

转载 作者:行者123 更新时间:2023-11-28 20:22:06 25 4
gpt4 key购买 nike

这是我的 spec.ts 文件。我被错误困住了:

Error: Can't resolve all parameters for RequestOptions: (?)

我还导入了所有必要的提供程序。谁能帮我解决这个错误?

import { async, ComponentFixture, TestBed, inject } from '@angular/core/testing';
import { ResetPasswordComponent } from './reset-password.component';
import { ConfigService } from './../config-service.service';
import {Http, Headers, ConnectionBackend, RequestOptions} from '@angular/http';

describe('ResetPasswordComponent', () => {
// let component: ResetPasswordComponent;
// let fixture: ComponentFixture<ResetPasswordComponent>;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [ResetPasswordComponent, ConfigService, Http, ConnectionBackend, RequestOptions]
});
});

// beforeEach(async(() => {
// TestBed.configureTestingModule({
// declarations: [ ResetPasswordComponent ]
// })
// .compileComponents();
// }));

beforeEach(() => {
fixture = TestBed.createComponent(ResetPasswordComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

// it('should create', () => {
// expect(component).toBeTruthy();
// });

it('should create', () => {
expect('holaa').toBe('holaa');
});

it('Is Password Change Function Working', inject([ResetPasswordComponent], (reset:ResetPasswordComponent) => {
expect(reset.simplyAFunction()).toBe(true);
}));
});

最佳答案

我在使用 HttpHeaders 时遇到了类似的情况,希望我的经验对您有所帮助。我的单元测试总是尝试模拟服务,无论是在构建服务中还是写给我们。

因此在这种情况下,您可以像这样模拟 RequestOptions。 将您的提供商更改为

providers: [ResetPasswordComponent, ConfigService, Http, ConnectionBackend, { provide: RequestOptions, useValue: RequestOptionsMock }]

通过在单元测试系统中执行此操作,系统将寻找模拟的 RequestOptions 而不是原始的 RequestOptions,然后我们可以编写我们自己的模拟 RequestOptions 类似的提供我们在代码中使用的属性

示例模拟对象可能是这样的

 const RequestOptionsMock = function(){
return 'mockedResponse';
}

补充说明这是 Angular 文档中的原始 RequestOptions https://angular.io/api/http/RequestOptions

class RequestOptions {
constructor(opts: RequestOptionsArgs = {})
method: RequestMethod | string | null
headers: Headers | null
body: any
url: string | null
params: URLSearchParams
search: URLSearchParams
withCredentials: boolean | null
responseType: ResponseContentType | null
merge(options?: RequestOptionsArgs): RequestOptions
}

关于 Angular Testing karma - 错误 : Can't resolve all parameters for RequestOptions: (?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49487919/

25 4 0