我已经创建了一个Angular库,其中包含一个简单的组件和一个服务,该服务执行HTTP调用并获取结果,然后将其消耗在该组件中。
所有工作都可以找到,除了测试不会因依赖项注入错误而通过:
Error: StaticInjectorError(DynamicTestModule)[HttpClient]:
StaticInjectorError(Platform: core)[HttpClient]:
NullInjectorError: No provider for HttpClient!
供参考,这是我的代码:
@Injectable({
providedIn: "root"
})
export class MyService {
public apiUrl = "https://apiurl";
constructor(private http: HttpClient) {}
getSomeData(key: string): Observable<string> {
return this.http.get(
`${this.apiUrl}/${key}`,
{ responseType: "text" }
);
}
}
测试:
describe('MyService', () => {
let injector : TestBed;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
MyService
],
imports: [
HttpClientTestingModule
],
});
injector = getTestBed();
});
it('should be created', () => {
const service = injector.get(MyService);
expect(service).toBeTruthy();
});
});
我的猜测是,这与@ angular-devkit有关,但我可能错了。是的,我尝试了所有变体,甚至尝试注入真正的http-client,该客户端也不起作用。
答案 0 :(得分:0)
我查看了您的存储库,发现您专注于function isAlphabetical(str) {
// we iterate only while i<length-1 to avoid going out of bounds when doing str[i+1]
for (let i = 0, n = str.length - 1; i < n; i++) {
if (str[i] > str[i + 1])
return false; // at least one letter is making it not ordered
// else keep checking other letters
}
return true; // if we reached here, its ordered
}
文件,但是忘记了在测试组件时没有导入ngx-pass-strength.service.spec.ts
。
ngx-pass-strength.component.spec.ts
HttpClientTestingModule