"ReferenceError: Can't find variable: Mustache" in Unit Tests?

"ReferenceError: Can't find variable: Mustache" in Unit Tests?

im 运行 一个 rails 应用程序,它在 Javascript 端进行 运行 单元测试(使用 Teaspoon/Jasmine)。

有趣的是,在我调用的函数上,我知道 Mustache.render 函数正在工作(我能够 console.log 它的 return 值(即 Mustache.render函数)并查看它是否正常工作。但是,当我从我的单元测试中调用该函数时,我得到一个:

Failure/Error: ReferenceError: Can't find variable: Mustache

作为参考,我实际上并没有直接调用 Mustache 渲染函数,我只是调用使用它的函数并获取它的 return 值以再次检查。

我已经能够成功地获取和使用各种其他功能并且使用它们很好,这个只是给我带来麻烦。 Mustache.render 对象不能存在于它自己的文件或范围之外吗?

编辑:示例代码:

_makeSomething: function viewMakeSomething(data) {
      const template = templates.something;

      return Mustache.render(document.querySelector(template).innerHTML, data);
    }

我的测试代码很简单:

  it('_makeSomething object', function() {

    let obj = {id: 1234, content: "_makeSomething Assertion", author: "Test User"}
    let $something = _makeSomething(obj);
  });

(现在我只是在断言或拆分它之前捕获它 up/etc...,但它只是调用它)

问题是您的 teaspoon 无法访问您的 dev/production 资产 pipelene。您应该指定要为您的测试加载什么 JS。这是防止从清单加载所有文件以测试某些功能所必需的。因为这是单元测试。

来自example

//= require mustache
describe("My great feature", function() {

  it("will change the world", function() {
    expect(true).toBe(true);
    expect(Mustache).toBeDefined();
  });

});