我尝试测试一个新类和构造函数,我有一个configService.get。
@Injectable()
export class StripeService {
private stripe: Stripe;
constructor(private configService: ConfigService) {
const secretKey = configService.get<string>('STRIPE_SECRET') || '';
this.stripe = new Stripe(secretKey, {
apiVersion: '2020-08-27',
});
}
}
这是我的测试课:
import { Test, TestingModule } from '@nestjs/testing';
import { StripeService } from './stripe.service';
import { ConfigService } from '@nestjs/config';
const mockStripe = () => ({})
describe('StripeService', () => {
let service: StripeService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [StripeService,
{ provide: ConfigService, useFactory: mockStripe}
],
}).compile();
service = module.get<StripeService>(StripeService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
当我尝试运行测试时,出现以下错误:
如果有人有解决方案的想法,我想解释一下。
由于ConfigService
通过提供工厂来覆盖提供程序,因此应get
在工厂函数内的返回对象上定义-function。尝试类似:
const mockStripe = () => ({get:() => undefined})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句