【问题标题】:Mocha assertion error: expected {} to be a stringMocha 断言错误:预期 {} 为字符串
【发布时间】:2016-09-14 18:38:46
【问题描述】:

我整个早上都在为此苦苦挣扎,我很确定我只是错过了一些简单的东西。当我期待一个字符串时,似乎我正在为返回值获取一个新对象 ({}),但即使我为返回值硬编码一个字符串,我也会得到相同的错误。

我已经顺利完成了examples found here。我的package.json 设置为正确测试(或者至少我认为这不是问题,但如果它有助于解决我的问题,我也可以发布它)。我是 Node.js 的新手(但在 JS 方面经验丰富)并且刚刚学习 Mocho 和 Chai。

我错过了什么?当我应该得到一个字符串时,为什么我得到一个似乎是空的对象?是什么导致测试失败?

我编写了一个简单的 API 来从主机 PC 获取用户名:

const username = require('username');
exports.getUserName = function() {
    console.log(username.sync());
    return username.sync();
};

我已经使用 Mocha & Chai 编写了一个测试:

var expect = require("chai").expect;
var getUserName = require('./username.js');

describe("User name API", function () {
it("Returns a string with the user's name", function () {
    expect(getUserName).to.be.a('string');
    });
});

这是我使用npm test 运行测试时返回的错误:

> sbserialwidget@0.0.1 test C:\deg\node_modules\sbSerialWidget
> mocha --reporter spec

running

  User name API
    1) Returns a string with the user's name

  0 passing (13ms)
  1 failing

  1) User name API Returns a string with the user's name:
     AssertionError: expected {} to be a string
      at Context.<anonymous> (C:\deg\node_modules\sbSerialWidget\test\username.js:6:29)
      at callFn (C:\deg\node_modules\sbSerialWidget\node_modules\mocha\lib\runnable.js:334:21)
      at Test.Runnable.run (C:\deg\node_modules\sbSerialWidget\node_modules\mocha\lib\runnable.js:327:7)
      at Runner.runTest (C:\deg\node_modules\sbSerialWidget\node_modules\mocha\lib\runner.js:429:10)
      at C:\deg\node_modules\sbSerialWidget\node_modules\mocha\lib\runner.js:535:12
      at next (C:\deg\node_modules\sbSerialWidget\node_modules\mocha\lib\runner.js:349:14)
      at C:\deg\node_modules\sbSerialWidget\node_modules\mocha\lib\runner.js:359:7
      at next (C:\deg\node_modules\sbSerialWidget\node_modules\mocha\lib\runner.js:285:14)
      at Immediate._onImmediate (C:\deg\node_modules\sbSerialWidget\node_modules\mocha\lib\runner.js:327:5)

如果我将测试更改为期望对象,则测试有效:expect(getUserName).to.be.an('object');

但是,如果我执行console.log(typeof username.sync());,它会说它是一个字符串。

我需要做什么来解决这个问题?


编辑解决方案:

这是我最终开始工作的代码。我认为部分问题是路径问题(我在 Windows 环境中),部分原因是我根本不明白需要做什么,最后我不明白如何在测试中正确调用函数(见下文) .

这是修改后的username.js 代码:

const username = require('username');
exports.getUserName = function() {
    console.log(username.sync());
    return username.sync();
}

这是修改后的usernametest.js

var expect = require("chai").expect;
//here's where one point of confusion was, I was trying to call the original getUserName()
//function, but it's been turned into a variable called username
var username = require('..\\js\\username.js').getUserName;

describe("User name API", function () {
    it("returns a string with the user's name", function () {
        //so here, instead of calling username.getUserName(), I call username()
        //instead.  Voila, it works...
        expect(username()).to.be.a('string');
    });
});

【问题讨论】:

    标签: node.js mocha.js


    【解决方案1】:

    你没有执行函数

    改变

    expect(getUserName).to.be.a('string');

    expect(getUserName()).to.be.a('string');

    编辑

    我不知道您正在导出一个对象

    exports.getUsername = function(){...}

    应该是

    expect(getUserName.getUserName()).to.be.a('string');

    感谢@robertklep

    【讨论】:

    • 不仅如此,username.js 还导出了一个带有 getUserName 函数的对象,所以要让它工作,它应该是 getUserName.getUserName()
    • 当我尝试这样做时,错误变为:TypeError: getUserName is not a function。此外,作为对@robertklep 的回应,使该更改产生了类似的错误:TypeError: getUserName.getUserName is not a function
    • @delliottg 加上@stalin 和我自己提出的修复,对我来说效果很好。
    • 嗯,不知道我在哪里搞砸了。我构建了一个全新的项目,复制了相关文件,在所需的库上运行 npm install,编辑 package.json 以指向正确的测试位置,但我仍然收到相同的 TypeError 消息。想法?
    • 感谢大家的帮助和建议,我终于找到了解决方案(参见编辑后的 ​​OP)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 2019-11-15
    相关资源
    最近更新 更多