javascript - 无法实例化新的 Javascript 类 - 测试失败
问题描述
为什么我的课程开始没有重置为默认配置?查看失败的测试。
类文件
// class file
const defaultConfig = {
color: 'blue',
texture: 'rough'
}
class BaseClass {
constructor(config) {
this.config = Object.assign({}, defaultConfig, config);
this.color = this.config.color
this.texture = this.config.texture
}
}
module.exports = BaseClass
测试文件
// test file
const BaseClass = require('./BaseClass')
describe('class', () => {
it('instantiates base', () => {
const base = new BaseClass({})
expect(base.color).toBe('blue')
expect(base.texture).toBe('rough')
})
it('instantiates smooth', () => {
const smooth = new BaseClass({ texture: 'smooth' })
expect(smooth.color).toBe('blue')
expect(smooth.texture).toBe('smooth')
})
it('instantiates green', () => {
const green = new BaseClass({ color: 'green' })
expect(green.color).toBe('green')
// FAILS! Texture
expect(green.texture).toBe('rough') // rough is the default, should be rough
})
})
解决方案
推荐阅读
- javascript - 如何使用 EventListener 更改元素的值
- python - Python Trio 设置十进制数的工人
- linux - ArchLinux 上的 I2C 用户空间通信
- c++ - 如何使用嵌套初始化构造函数中的一维向量初始化矩阵?
- node.js - 如何在 Mongoose 中保存生日而不引发验证错误
- c - 为什么快速排序函数中的变量“q”在第一个过程(调用)后等于 7
- python - 我可以在 Pandas 中将 getattr 与方法链一起使用吗?
- python - 在 spaCy 中,有没有办法提取提取实体的句子?
- firebase - 收听对象数组流时出现问题来自firestore集合
- c - 回调未在编辑控件上触发