javascript - 测试子类的方法
问题描述
我正在尝试测试 A 类的方法 X 调用导入的函数 Y。A 类是 B 类的子类,应该模拟出来。
A 类如下所示:
const B = require('./B');
const { Y } = require('../util');
class A extends B {
constructor() {
super('/A');
this.setCors('');
this.app.post('', this.X.bind(this));
}
X(req, res) {
Y();
}
}
module.exports = A;
测试尝试(遵循Jest Official Docs):
const A = require('../path/to/A');
const B = require('../path/to/B');
jest.mock('../path/to/B', () => {
return jest.fn().mockImplementation(() => {
return { setCors: jest.fn(), app: { post: jest.fn() } };
});
});
test('method X calls function Y', () => {
(new A()).X();
expect(Y).toBeCalled();
});
这给出了TypeError: Cannot read property 'bind' of undefined
关于 A 的构造函数的错误。
也许有必要只模拟构造函数,但我不知道该怎么做。
解决方案
这是解决方案:
文件夹结构:
.
├── A.spec.ts
├── A.ts
├── B.ts
└── util.ts
A.ts
:
import B from './B';
import { Y } from './util';
export default class A extends B {
constructor() {
super('/A');
this.setCors('');
this.app.post('', this.X.bind(this));
}
public X(req, res) {
Y();
}
}
B.ts
:
export default class B {
public app = {
post(route: string, controller: () => any) {
//
}
};
private name: string = '';
private url: string = '';
constructor(name: string) {
this.name = name;
}
protected setCors(url: string) {
this.url = url;
}
}
util.ts
:
export function Y() {
console.log('I am Y');
}
A.spec.ts
:
import A from './A';
import { Y } from './util';
jest.mock('./util.ts', () => {
return {
Y: jest.fn()
};
});
const a = new A();
describe('A', () => {
it('should execute function Y correctly', () => {
const mockedReq = {};
const mockedRes = {};
a.X(mockedReq, mockedRes);
expect(Y).toBeCalledTimes(1);
});
});
具有 100% 覆盖率报告的单元测试结果:
PASS src/stackoverflow/52075711/A.spec.ts (5.747s)
A
✓ should execute function Y correctly (8ms)
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
A.ts | 100 | 100 | 100 | 100 | |
B.ts | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 9.623s, estimated 14s
源代码:https ://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/52075711
推荐阅读
- java - 关闭连接后出现“java.sql.SQLException:连接已关闭”
- android - 为什么我的模拟器显示这个图形故障,之前它工作正常
- python-3.x - 在python中使用OCR从图像中提取特定的文本块
- r - 在 R 中使用 RNN (Keras) 进行时间序列预测
- javascript - 使用 puppeteer 调整浏览器大小
- tensorflow - 为什么 Keras API 需要第一层的输入形状,因为没有它它实际上工作得很好?
- android - 如何从editText中的文本中删除红线并在android studio中按Enter后防止和editTextView扩展
- php - 如果类别行为空,如何显示默认图像
- javascript - 将参数值组合到 Jquery 中的变量名
- vba - 哪种做法更好:使用更多数据的 IF 语句更少,还是相反?