我有一项服务可以从@cloudinary/angular-5.x
注入cloudinary类。
如何为我的服务提供模拟的cloudinary服务?
这是我的测试
import { TestBed } from '@angular/core/testing';
import { Cloudinary } from '@cloudinary/angular-5.x';
import { Cloudinary as CloudinaryCore } from 'cloudinary-core';
import { ProductImageService } from './product-image.service';
import { HttpTestingController, HttpClientTestingModule } from '@angular/common/http/testing';
import { CloudinaryService } from './cloudinary.service';
fdescribe('ProductImageService', () => {
let httpMock: HttpTestingController;
const baseUrl = 'https://test.com/';
beforeEach(() => {
const cloudinaryLib = {
Cloudinary: CloudinaryCore
};
const mockCloudinary = new MockCloudinary(cloudinaryLib, {
cloud_name: 'test_cloud',
api_key: 'test_api_key',
api_secret: 'api_secret'
});
TestBed.overrideProvider(Cloudinary, { useFactory: () => mockCloudinary });
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
ProductImageService,
CloudinaryService,
{ provide: 'BASE_URL', useValue: baseUrl }
]
});
httpMock = TestBed.get(HttpTestingController);
});
it('should be created', () => {
const service: ProductImageService = TestBed.get(ProductImageService);
expect(service).toBeTruthy();
});
});
class MockCloudinary extends Cloudinary {}
CloudinaryService
是我注入到ProductImageService
中的一项服务,其中CloudinaryService
注入了Cloudinary
类。
运行测试时,我得到以下结果:
Chrome 71.0.3578 (Windows 10.0.0) ProductImageService should be created FAILED
Error: StaticInjectorError(DynamicTestModule)[CloudinaryService -> Cloudinary]:
StaticInjectorError(Platform: core)[CloudinaryService -> Cloudinary]:
NullInjectorError: No provider for Cloudinary!
我正在使用angular
7.2,@cloudinary/angular-5.x
1.0.2和cloudinary-core
2.5.0。