使用 jasmine-node 进行测试:理解错误

Testing with jasmine-node : understanding error

我正在尝试通过在 exercism.io 上做一些练习来学习 javascript(它提供了我们需要通过的预写测试)。有一段时间一切顺利,但通过最新的练习,我似乎无法弄清楚测试是如何进行的。

本练习的目的是编写一个程序,计算给定字符串在另一个字符串中出现的次数。这是测试文件的样子:

var words = require('./word-count');

describe("words()", function() {
  it("counts one word", function() {
    var expectedCounts = { word: 1 };
    expect(words("word")).toEqual(expectedCounts);
  });

  // more skipped tests  

});

我做的第一件事是创建 word-count.js 文件,但我不知道该文件中的代码如何 formatted/setup 才能被规范文件读取。浏览 this tutorial 后,我设置 word-count.js 如下:

exports = function(str){}

但是这(以及我尝试过的所有其他方法)给了我错误

  1) words() counts one word
   Message:
     TypeError: object is not a function
   Stacktrace:
     TypeError: object is not a function
    at null.<anonymous> (/home/sheeka/exercism/javascript/word-count/word-count_test.spec.js:6:12)

我不明白这个错误,也不明白它在寻找什么。我也不明白测试是如何工作的,因为在我看来规范文件没有调用 word-count.js

中的任何函数或变量

如果能帮助我理解这一点或指出我可以用来解决这个问题的资源方向,我们将不胜感激。我确实尝试了 运行 和 google 搜索,但我发现的所有内容对于我的目的来说看起来都太高级了。

分配 exports = function(str){} 不会使您的函数 public。请尝试 module.exports = function(str){}。 或者,您可以将函数分配为 exports prop exports.fc = function() {...};

默认情况下 exports 是一个指向 module.exports 的空对象。当您覆盖 exports 时,module.exports 保持不变。