测试子类的方法
Test the method of a subclass
我正在尝试测试 class A 的方法 X 调用导入的函数 Y。Class A 是 class B 的子 class,应该是被嘲笑了。
Class 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();
});
这给出了关于 A.
构造函数的错误 TypeError: Cannot read property 'bind' of undefined
也许有必要只模拟构造函数,但我不确定该怎么做。
解决方法如下:
文件夹结构:
.
├── 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/Whosebug/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/Whosebug/52075711
我正在尝试测试 class A 的方法 X 调用导入的函数 Y。Class A 是 class B 的子 class,应该是被嘲笑了。
Class 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();
});
这给出了关于 A.
构造函数的错误TypeError: Cannot read property 'bind' of undefined
也许有必要只模拟构造函数,但我不确定该怎么做。
解决方法如下:
文件夹结构:
.
├── 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/Whosebug/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/Whosebug/52075711