【发布时间】:2019-11-15 08:25:15
【问题描述】:
我是 typescript 的新手,我不明白为什么会出错,最重要的是如何调试它。
这是我项目的结构:
lpuggini@lpuggini-T3420:~/pisoreview$ ls
jest.config.js node_modules package.json package-lock.json src tsconfig.json
lpuggini@lpuggini-T3420:~/pisoreview$ tree src/
src/
├── common
│ ├── apartment.js
│ ├── apartment.ts
│ ├── firestore_manager.ts
│ ├── review.js
│ ├── review.ts
│ ├── tests
│ │ └── apartment.test.ts
│ ├── user.js
│ └── user.ts
├── frntd
│ └── frontend.ts
├── index.js
└── index.ts
3 directories, 11 files
lpuggini@lpuggini-T3420:~/pisoreview$
当我运行单元测试时,我收到以下错误:
lpuggini@lpuggini-T3420:~/pisoreview$ npm t
> pisoreview@1.0.0 test /home/lpuggini/pisoreview
> jest
FAIL src/common/tests/apartment.test.ts
✕ create apartment (5ms)
● create apartment
TypeError: ap1.toSet is not a function
6 | console.log(ap1)
7 | debugger;
> 8 | var s = ap1.toSet();
| ^
9 | expect(s['city']).toBe('Madrid');
10 | })
11 |
at Object.<anonymous> (src/common/tests/apartment.test.ts:8:17)
console.log src/common/tests/apartment.test.ts:6
Apartment {
country: 'Spain',
city: 'Madrid',
street: 'calle amalia',
number: 18,
flat: 3,
door: 'B' }
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 0.872s, estimated 1s
Ran all test suites.
npm ERR! Test failed. See above for more details.
lpuggini@lpuggini-T3420:~/pisoreview$
失败的测试在哪里:
import { Apartment } from '../apartment';
test('create apartment', () => {
let ap1 = new Apartment('Spain', 'Madrid', 'calle amalia', 18, 3, 'B');
console.log(ap1)
debugger;
var s = ap1.toSet();
expect(s['city']).toBe('Madrid');
})
lpuggini@lpuggini-T3420:~/pisoreview$
公寓文件是:
lpuggini@lpuggini-T3420:~/pisoreview$ cat src/common/apartment.ts
export class Apartment {
country: string
city: string
street: string
number: number
flat: number
door: string
reference: string
constructor(country:string, city:string, street:string, number:number, flat:number, door:string) {
this.country = country
this.city = city
this.street = street
this.number = number
this.flat = flat
this.number = number
this.door = door
}
public toString() {
return ` ${this.country}_${this.city}_${this.street}_${this.flat}_${this.number}_${this.door}`
}
public toSet() {
return {
"country": this.country,
"city": this.city,
"street": this.street,
"number": this.number,
"flat": this.flat,
"door": this.door,
};
}
}
lpuggini@lpuggini-T3420:~/pisoreview$
现在的问题是:
1)为什么会失败?
2)如何调试代码? python中有nosetests --pdb之类的东西吗?
非常感谢
【问题讨论】:
标签: node.js typescript unit-testing