简体   繁体   English

如何编写测试,没有mocking 的nestjs 中的存储库

[英]How to write a test, without mocking the repository in nestjs

I've started working on a small project and I'm now in the process of writing tests for this project, But the problem now I'm facing is related to injecting the repository.我已经开始做一个小项目,我现在正在为这个项目编写测试,但我现在面临的问题与注入存储库有关。 I have the following code in beforeEach function我在 beforeEach function 中有以下代码

const moduleFixture = await Test.createTestingModule({
            controllers:[CustomersController],
            providers:[
                CustomersService,
                {
                    provide: getRepositoryToken(File),
                    useValue: Repository
                },
            ]
        }).compile();

And I have a test which it calls an endpoint, Now every time I run the tests, This error is showing up我有一个调用端点的测试,现在每次运行测试时,都会出现这个错误

Cannot read property 'find' of undefined

And it's obvious because I have a code in my CustomerService class, this.fileRepository.find() , And the reason it throws the error because the file repository has no methods which any other repository should have.这很明显,因为我的 CustomerService class 中有一个代码this.fileRepository.find() ,它抛出错误的原因是因为文件存储库没有任何其他存储库应该有的方法。 在此处输入图像描述

I'll appreciate any help, Thanks.我会很感激任何帮助,谢谢。

If you're wanting to use Repository you should be using useClass instead of useValue .如果你想使用Repository你应该使用useClass而不是useValue You can see several examples of Repository mocking in this GitHub repo您可以在此 GitHub 存储库中查看存储库 mocking 的几个示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM