嗨,在使用testBed.configureModule进行测试时,我需要向构造函数传递两个参数
这是我的base.service.spec.ts
import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { BaseService } from '@shared/services/base/base.service';
import { HttpHeaders, HttpClient } from '@angular/common/http';
describe('BaseService', () => {
let baseService: BaseService<any>, httpClientTestingModule: HttpClientTestingModule;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [BaseService,
{ provide: HttpClientTestingModule, useValue: httpClientTestingModule },
{ provide: String, useValue: 'base' }
]
})
baseService = TestBed.get(BaseService)
//httpClientTestingModule = TestBed.get(HttpClientTestingModule)
})
const GET_BASE_URL = 'getBaseURL'
//Test getBaseUrl
it(`${GET_BASE_URL} should return a string`, () => {
baseService.getBaseURL().then((base) => {
if (base) {
//PASSED if base is a string
expect(base).toEqual(jasmine.any(String), 'Result is not a string')
} else {
fail(`${GET_BASE_URL} failed: string was empty or null`)
}
}, (error) => {
fail(`${GET_BASE_URL} failed: promise was not resolved`)
})
})
const GET_PROVIDER_HEADER = 'getProviderOnlyHeader'
//Test getProviderOnlyHeader
it(`${GET_PROVIDER_HEADER} should return HttpHeaders`, () => {
let result = baseService.getProviderOnlyHeader()
expect(result).toEqual(jasmine.any(HttpHeaders), 'Result is not a HttpHeader')
})
})
这是我的base.service.ts的一部分
export class BaseService<T> {
nativeStorage: NativeStorage;
//A single object
_item = new BehaviorSubject<T>(null);
//A list of items
_list = new BehaviorSubject<T[]>(null);
get item() {
return this._item.asObservable();
}
getCurrentItem() {
return this._item.getValue()
}
getCurrentList() {
return this._list.getValue()
}
get list() {
return this._list.asObservable();
}
constructor(public http: HttpClient, public controllerName: string) {
this.nativeStorage = new NativeStorage();
this.getBaseURL().then((base) => {
BASE = base
})
}
}
我知道也有类似的问题,但找不到解决方案 我的testBed设置应如何显示?
需要任何详细信息,请提前告知我
也收到此错误消息
错误:无法解析BaseService的所有参数:(?,?)。
基本服务也是超类,它不是真正的服务 基本服务参数被注入到super方法中,下面的示例
@Injectable({
providedIn: 'root'
})
export class AccessService extends BaseService<any> {
private _providers = new BehaviorSubject<any>(null);
get providers() {
return this._providers.asObservable();
}
currentProviders() {
return this._providers.getValue();
}
constructor(http: HttpClient) {
super(
http,
controllerName)
this.getBaseURL().then((base) => {
BASE = base
})
}
getProviders() {
return this.http.get<any>(BASE + docketName + environment.API_V2 + 'providers')
.pipe(
map(resp => {
return resp.content
}),
tap(result => {
this._providers.next(result)
})
);
}
}