【问题标题】:Testing JavaScript code from the command line [duplicate]从命令行测试 JavaScript 代码 [重复]
【发布时间】:2018-06-13 06:15:51
【问题描述】:

作为一个 JavaScript 新手,我很难理解如何“加载”我的代码,以便我可以在应用程序之外使用单元测试对其进行测试。

在 PHP 中,我有一个自动加载器(composer),所以我可以引用我想要与之交互的类并自动加载它,或者我可以使用require_once 来包含一个物理文件和其中的类或函数文件可供我使用。

所以,其中一个类(存储在文件 src/lib/App/Calculator.js 中)...

App.Calculator = (function () {

    var Calculator = function (onUpdateCallback) {
        this.amountChange = 0.00;
        this.amountDue = 0.00;
        this.amountTendered = 0.00;
        this.onUpdateCallback = onUpdateCallback;
    };

    Calculator.prototype = {
        clear: function () {
            this.amountTendered = 0.00;
            this.calculate();
        },

        calculate: function () {
            if (this.amountDue > 0) {
                this.amountChange = Math.max(0, this.getAmountTendered() - this.amountDue);
            } else {
                this.amountChange = -(this.getAmountTendered() + this.amountDue);
            }

            if (typeof this.onUpdateCallback === 'function') {
                this.onUpdateCallback(this);
            }
        },

        getAmountTendered: function () {
            return Number(this.amountTendered);
        },

        getChange: function () {
            return this.amountChange;
        },

        setAmountTendered: function (amountTendered) {
            this.amountTendered = parseFloat(amountTendered);
            this.calculate();
        },

        setAmountDue: function (amountDue) {
            this.amountDue = parseFloat(amountDue);
            if (this.amountDue < 0) {
                this.negative = true;
            }
            this.calculate();
        }
    };

    return Calculator;

})();

我希望能够创建 App.Calculator 的新实例并在应用程序之外对其进行测试。从命令行。

从 PHPUnit 到 JS,我知道我缺少很多理解,所以任何指针(自以为是或其他)都将不胜感激。

【问题讨论】:

  • 如果你只是在浏览器中打开你的网站,你应该可以访问App.Calculator,然后做你想做的事。
  • 在浏览器中打开你的应用程序 -> f12 -> 来源 -> 找到 js 文件并在你的类中放置任意数量的断点和重新加载页面或 f12 -> var calc = App.calculator (); calc.getChange(); ...无论您想在课堂上测试什么;
  • 如果您正在寻找 JUnit 测试框架,我可以推荐 Facebook 的 Jest。我基本上随附电池,并且非常容易设置。
  • @RoumelisGeorge 这个问题是关于运行单元测试的。这与调试代码或在浏览器中手动测试无关。
  • 我在我的 nodejs 项目中使用 mocha / chai。关于在这些测试中导入代码,我建议使用基于模块的方法,请参阅:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 如果代码打算在浏览器中运行,您可能必须使用像 babel 或 bundler 这样的转译器作为 webpack。

标签: javascript unit-testing


【解决方案1】:

最好的办法是使用 F12 调试命令行在浏览器中测试您的代码。

如果您想在实际命令行中执行此操作,另一种选择是使用 Node.JS。这基本上是一个 javascript 的后端应用程序,您只需将代码粘贴到 .js 文件中,然后使用 CMD 命令:"node [yourfile.js]",它将运行该文件。使用console.log 可以登录到命令行。

【讨论】:

  • 手动复制/粘贴代码到浏览器的命令行中以运行单元测试是一个糟糕的主意。工作量太大了。
  • “然后使用 CMD 命令”——这对解决问题没有帮助。它不会对代码进行任何测试
  • 我正在尝试自动化测试运行。几百个班。这只是一个例子。我们在管道中使用 PHPUnit,它正在测试超过 20,000 个测试。我们现在想要对 JS 代码进行一些测试。我们都不是专家,所以我们知道我们将不得不学习一些东西。不应涉及浏览器。
  • 问题是关于测试套件的。不是通过日志驱动的方法手动调试代码。单元测试!= 在浏览器中测试
  • @RichardAQuadling 啊,使用节点你可以要求代码是的。您可以使用require("./filename.js"); 执行此操作,但是在您需要的文件中,您将以下内容放在底部:module.exports = 后跟要导出的内容。这可以是任何东西。如果您随后需要该文件,它将包含您使用module.exports 导出的任何内容。注意:如果您使用不在 module.exports 中的函数,它将不起作用,除非该函数由 module.exports 中的代码调用。示例:gist.github.com/PauldeKoning/2077c08e80c38f79203825eefda7bd11
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-28
  • 2011-05-30
  • 2011-03-18
  • 2010-10-29
  • 2014-04-24
  • 2014-05-30
  • 1970-01-01
相关资源
最近更新 更多