抛出新错误是我的语法错误吗?我的测试失败了...不确定为什么

问题描述

我是Java语言的新手,目前正在学习有关类和测试的知识。我的测试在测试错误时失败。我的课程代码,测试代码和测试结果如下。

很抱歉,如果我缺少明显的东西,我是一个很新的人,已经被扔进去了。

班级:

class Bag {
    constructor(weight) {
        if (!weight) throw new Error("bag must have a weight")
        this.weight = weight
    }
}


module.exports = Bag

测试代码:

const { TestScheduler } = require("jest")

const Bag = require("./bag")

describe("bag",() => {
    test("has a weight",() => {
        const bag = new Bag(13)
        expect(bag.weight).toBe(13)
    })
    test("bag must have a weight",() => {
        const bag = new Bag()
        expect(() => new Bag()).toThrowError("bag must have a weight")
    })
})

测试结果:

Debugger attached.
 FAIL  ./airport.test.js
  bag
    ✓ has a weight (1 ms)
    ✕ bag must have a weight (1 ms)

  ● bag › bag must have a weight

    bag must have a weight

      2 |     constructor(weight) {
      3 |         if (!weight) {
    > 4 |             throw new Error("bag must have a weight")
        |                   ^
      5 |         }
      6 |         this.weight = weight
      7 |     }

      at new Bag (bag.js:4:19)
      at Object.<anonymous> (airport.test.js:11:21)

  console.log
    16

      at Object.<anonymous> (bag.js:11:9)

Test Suites: 1 failed,1 total
Tests:       1 failed,1 passed,2 total
Snapshots:   0 total
Time:        1.166 s
Ran all test suites.
Waiting for the debugger to disconnect...
npm ERR! Test failed.  See above for more details.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)