[英]How check that method was called with correct param in unit test SinonJs?
How I can check that methods this.athena.startQueryExecution(param) was called with correct param in unit test using sinon?我如何检查this.athena.startQueryExecution(param)在使用 sinon 的单元测试中使用正确的参数调用的方法? And is it possible?
有可能吗?
I have a simple code, which runs query to database:我有一个简单的代码,它运行对数据库的查询:
export class QueryService {
private readonly athena: Athena = new Athena();
constructor() {
}
public async query(date?: Date) {
const param = {
QueryString: this.generateQuery(date),
};
await this.athena.startQueryExecution(param)
.promise();
}
private generateQuery(date?: Date): string {
if (date) {
return `SELECT * FROM TABLE
WHERE date='$date'
} else {
return `SELECT * FROM TABLE
WHERE date='any date'
}
}
My test:我的测试:
describe('QueryService', () => {
let queryService = new QueryService();
describe('Query', () => {
it('test', async () => {
// given
const date: Date = {date: "2019-01-01"};
// where
queryService.query(date);
// then
});
});
});
Unit test solution:单元测试解决方案:
queryService.ts
: queryService.ts
:
import { Athena } from './athena';
export class QueryService {
private readonly athena: Athena = new Athena();
constructor() {}
public async query(date?: Date) {
const param = {
QueryString: this.generateQuery(date),
};
return this.athena.startQueryExecution(param).promise();
}
private generateQuery(date?: Date): string {
if (date) {
return `SELECT * FROM TABLE WHERE date='$date'`;
} else {
return `SELECT * FROM TABLE WHERE date='any date'`;
}
}
}
athena.ts
: athena.ts
:
export class Athena {
public startQueryExecution(param) {
return this;
}
public async promise() {
return 'real data';
}
}
queryService.test.ts
: queryService.test.ts
:
import { QueryService } from './queryService';
import { Athena } from './athena';
import sinon from 'sinon';
describe('60997196', () => {
afterEach(() => {
sinon.restore();
});
it('should pass', async () => {
const startQueryExecutionStub = sinon.stub(Athena.prototype, 'startQueryExecution').returnsThis();
const promiseStub = sinon.stub(Athena.prototype, 'promise').resolves('fake data');
const queryService = new QueryService();
const date: Date = new Date('2019-01-01');
const actual = await queryService.query(date);
sinon.assert.match(actual, 'fake data');
sinon.assert.calledWithExactly(startQueryExecutionStub, {
QueryString: `SELECT * FROM TABLE WHERE date='$date'`,
});
sinon.assert.calledOnce(promiseStub);
});
});
unit test results with coverage report:带有覆盖率报告的单元测试结果:
60997196
✓ should pass
1 passing (29ms)
-----------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------------|---------|----------|---------|---------|-------------------
All files | 72.73 | 50 | 60 | 72.73 |
athena.ts | 33.33 | 100 | 0 | 33.33 | 3,6
queryService.ts | 87.5 | 50 | 100 | 87.5 | 18
-----------------|---------|----------|---------|---------|-------------------
source code: https://github.com/mrdulin/expressjs-research/tree/master/src/stackoverflow/60997196源代码: https : //github.com/mrdulin/expressjs-research/tree/master/src/stackoverflow/60997196
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.