2017-04-26 27 views
0

即時通訊運行一個rails應用程序,即時通訊運行單元測試在Javascript端(使用茶匙/茉莉花)。單元測試中的「ReferenceError:Can not find variable:Mustache」?

有趣的是,在函數我打電話我知道Mustache.render功能正在工作(我能夠console.log它的返回值(這是Mustache.render函數),並看到它正在工作。但是,當我調用該函數從我的單元測試即時得到一個:

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

僅供參考我真的不叫鬍子渲染功能直接IM簡單地調用使用它的功能,並抓住它的返回值再次檢查

我已經能夠成功地gr ab和使用各種其他功能,並使用它們就好,這只是給我麻煩。 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); 
    }); 

(現在我只是在拍攝之前,我斷言任何東西,或分裂它的/ etc ...但它只是在調用它)

+0

你能顯示代碼,好嗎? – Torben

+0

當然,我會添加一個編輯。 – msmith1114

回答

1

問題是您的茶匙無法訪問您的開發/生產資產pipelene。您應該指定要爲您的測試加載的JS。這是防止從清單載入所有文件以測試某些功能所必需的。因爲這是單元測試。

example

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

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

}); 
+0

我注意到了文件本身(它使用'Mustache'並不需要它,也沒有任何'import file'需要它)。我知道它只是作爲npm軟件包安裝的......但我沒有看到它被包含在任何地方。 – msmith1114

+0

我嘗試在我的spec_helpers文件中添加'// must require Mustache'(這是茶匙建議的),不幸的是運氣不爲零 – msmith1114

+0

根據[docs](https://github.com/mustache/mustache)'Mustache'一個ruby類,你不能像在js文件中那樣調用它。你需要找到一些適配器來做到這一點或適當的寶石。 – nautgrad