首页 > 解决方案 > 无法实例化新的 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
  })
})

标签: javascriptclass

解决方案


推荐阅读