Node.js 如何测试具有本地依赖关系的TypeORM存储库方法
我是Node的新手,我正在尝试使用Mocha和Sinon测试TypeORM自定义存储库,而不会影响数据库 我的存储库有一个接受2个参数并返回承诺的方法。它使用一个本地查询生成器,我想监视它(queryBuilder),以了解它的方法被调用了多少次。这是我的自定义存储库:Node.js 如何测试具有本地依赖关系的TypeORM存储库方法,node.js,unit-testing,mocha.js,sinon,typeorm,Node.js,Unit Testing,Mocha.js,Sinon,Typeorm,我是Node的新手,我正在尝试使用Mocha和Sinon测试TypeORM自定义存储库,而不会影响数据库 我的存储库有一个接受2个参数并返回承诺的方法。它使用一个本地查询生成器,我想监视它(queryBuilder),以了解它的方法被调用了多少次。这是我的自定义存储库: @EntityRepository(Pratica) export class PraticaRepository extends Repository<Pratica> { list(targa?: s
@EntityRepository(Pratica)
export class PraticaRepository extends Repository<Pratica> {
list(targa?: string, tipoVeicolo?: string): Promise<Pratica[]> {
fileLogger.log('info','inizio - targa: %s; tipoVeicolo %s.', targa, tipoVeicolo);
let queryBuilder: SelectQueryBuilder<Pratica> = this.createQueryBuilder("p")
.leftJoinAndSelect("p.stato", "stato")
.leftJoinAndSelect("p.microstato", "microstato");
let filtered: boolean = false;
if(targa && targa !== ""){
fileLogger.debug("Applico filtro targa");
filtered = true;
queryBuilder.where("p.targa = :targa", {targa: targa});
}
if(tipoVeicolo && tipoVeicolo !== ""){
if(!filtered){
fileLogger.debug("Applico filtro tipoVeicolo");
filtered = true;
queryBuilder.where("p.tipoVeicolo = :tipoVeicolo", {tipoVeicolo: tipoVeicolo});
}else{
fileLogger.debug("Applico filtro tipoVeicolo come parametro aggiuntivo");
queryBuilder.andWhere("p.tipoVeicolo = :tipoVeicolo", {tipoVeicolo: tipoVeicolo});
}
}
fileLogger.log('debug', "Sql generato: %s", queryBuilder.getSql);
fileLogger.info("fine");
return queryBuilder.getMany();
}
但很明显,我得到了以下错误:
1) PraticaRepository#list
should call getMany once:
TypeError: Cannot read property 'createQueryBuilder' of undefined
at PraticaRepository.Repository.createQueryBuilder (src\repository\Repository.ts:50:29)
at PraticaRepository.list (src\repositories\PraticaRepository.ts:12:62)
因为我正在存根的查询生成器不是在存储库方法中实例化的。我的问题是:
- 有可能侦察到这样的方法吗
- 这种方法是“单元可测试”的吗?或者我应该只针对一些功能/集成测试进行测试
提前感谢。感谢@oligofren的建议,这是我的最终解决方案:
let sandbox;
let createQueryBuilderStub;
let mock;
let fakeQueryBuilder = new SelectQueryBuilder<Pratica>(null);
beforeEach(() => {
sandbox = sinon.createSandbox();
mock = sandbox.mock(fakeQueryBuilder);
createQueryBuilderStub = sandbox.stub(Repository.prototype,
'createQueryBuilder').withArgs("p").returns(fakeQueryBuilder);
});
afterEach(() => {
sandbox.restore();
});
describe('PraticaRepository#list', () => {
it('should get the result with no filters', async () => {
mock.expects('leftJoinAndSelect').twice().returns(fakeQueryBuilder);
mock.expects('where').never();
mock.expects('andWhere').never();
mock.expects('getSql').once();
mock.expects('getMany').once();
let cut = new PraticaRepository();
const appo = cut.list();
sinon.assert.calledOnce(createQueryBuilderStub);
mock.verify();
});
})
let沙箱;
让createQueryBuilderStub;
让我们嘲笑;
让fakeQueryBuilder=new SelectQueryBuilder(null);
在每个之前(()=>{
sandbox=sinon.createSandbox();
mock=sandbox.mock(fakeQueryBuilder);
createQueryBuilderStub=sandbox.stub(Repository.prototype,
'createQueryBuilder')。带有参数(“p”)。返回(fakeQueryBuilder);
});
之后(()=>{
sandbox.restore();
});
描述(‘实践知识库#列表’,()=>{
它('should get result with no filters',async()=>{
expected('leftJoinAndSelect').tweep().returns(fakeQueryBuilder);
mock.expected('where').never();
mock.expected('andWhere').never();
mock.expects('getSql').once();
mock.expects('getMany').once();
let cut=new pratica repository();
const appo=cut.list();
sinon.assert.calledOnce(createQueryBuilderStub);
mock.verify();
});
})
此答案给出了实现此目标的几个选项:。如果你想知道如何在某种意义上应用它,一定要说出来。您可以使用多种手动DI技术或proxyquire。请查看我们的操作指南:谢谢,根据您的建议,我现在有了一个测试的工作版本!
let sandbox;
let createQueryBuilderStub;
let mock;
let fakeQueryBuilder = new SelectQueryBuilder<Pratica>(null);
beforeEach(() => {
sandbox = sinon.createSandbox();
mock = sandbox.mock(fakeQueryBuilder);
createQueryBuilderStub = sandbox.stub(Repository.prototype,
'createQueryBuilder').withArgs("p").returns(fakeQueryBuilder);
});
afterEach(() => {
sandbox.restore();
});
describe('PraticaRepository#list', () => {
it('should get the result with no filters', async () => {
mock.expects('leftJoinAndSelect').twice().returns(fakeQueryBuilder);
mock.expects('where').never();
mock.expects('andWhere').never();
mock.expects('getSql').once();
mock.expects('getMany').once();
let cut = new PraticaRepository();
const appo = cut.list();
sinon.assert.calledOnce(createQueryBuilderStub);
mock.verify();
});
})