【问题标题】:Running Jasmine test not finding the class运行 Jasmine 测试未找到课程
【发布时间】:2021-02-15 02:08:48
【问题描述】:

我已经设置 Jasmine 在 JS 中运行我的测试。测试在 Specrunner 中运行和工作,但我想在命令行中使用 jasmine 来运行测试,但我得到了

1) Account Account balance to be 0
  Message:
    ReferenceError: Account is not defined
  Stack:
    ReferenceError: Account is not defined
        at UserContext.<anonymous> (/Users/student/Projects/week_9/tech_tests/bank_tech_test/spec/AccountSpec.js:4:27)
        at <Jasmine>
        at processImmediate (internal/timers.js:456:21)

1 spec, 1 failure
Finished in 0.01 seconds

我的 src 中有 Account 类,它可以在运行 Specrunner.html 时通过。 这是我的测试:

describe("Account", function(){
    describe("Account balance", function(){
        it("to be 0", function(){
            let account = new Account
            expect(account.balance).toBe(0)
        })
    })
})

我的 Jasmine 设置是否遗漏了什么?

编辑: 添加账号代码和文件路径

class Account{
    constructor(){
        this.balance = 0
    }
}

我的 Account.js 文件路径是 src/Account.js。 我的 AccountSpec.js 路径是 spec/AccountSpec.js

【问题讨论】:

    标签: javascript node.js unit-testing npm jasmine


    【解决方案1】:

    使用jasmine时,会自动加载一些模块(例如describeitexpect..)。

    但在您的情况下,您使用 Account 类。所以需要在测试中导入才可以使用!

    如果您使用的是纯 NodeJS,您将需要以下内容:

    1. 导出你的类,在文件末尾添加module.exports = { Account: Account }
    2. 在测试中导入你的类,在文件开头添加const { Account } = require('../src/Account')

    阅读更多关于export/import for NodeJS的信息。

    【讨论】:

    • 我不太确定我是否理解在 JS 中导入的方式。我对这门语言还很陌生,所以可能有些东西我不太明白
    • 所以你不使用任何捆绑器(如 webpack)或类似 RequireJS 的东西?你如何包含来自另一个文件的代码?如果您能提供更多关于您使用的内容的详细信息,将会有所帮助。
    • 我刚刚通过执行 npm jasmine 安装了 jasmine。我假设我缺少一些东西来将我的测试连接到 npm 像这样 requireJS?我有点困惑,因为设置指南没有提到这些事情。我假设只需安装 jasmine 并运行它
    • 如果Account 类已导出,请尝试添加require statemenet。类似于:const Account = require('../account.js')。如果您可以编辑您的问题并添加 Account 文件内容和文件路径,也会有所帮助。
    • 好的,我已经编辑了这些东西,我会在我的规范中尝试要求
    猜你喜欢
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 2021-10-25
    • 2020-03-25
    • 2021-08-04
    • 1970-01-01
    相关资源
    最近更新 更多