2015-05-04 57 views
0

我想在我的ember-cli應用程序中使用sinon,我無法弄清楚如何導入它。我在規範import語句:如何將sinon導入ember-cli測試?

import { sinon } from 'sinon'; 

,然後我嘗試在測試中使用興農:

it('finds the todo model', function() { 
    var store = this.subject().store; 
    var spy = sinon.spy(store, 'find'); 

    this.subject().model(); 

    expect(spy).to.have.been.called; 
}); 
然而

,當我從命令行運行ember test我得到這個錯誤:

not ok 8 PhantomJS 1.9 - TestLoader Failures my-app/tests/unit/routes/application-test: could not be loaded 
--- 
    message: > 
     Could not find module `sinon` imported from `my-app/tests/unit/routes/application-test` 
    stack: > 
     Error: Could not find module `sinon` imported from `my-app/tests/unit/routes/application-test` 
      at requireFrom (http://localhost:7357/assets/vendor.js:119) 
      at reify (http://localhost:7357/assets/vendor.js:106) 
      at http://localhost:7357/assets/vendor.js:149 
      at tryFinally (http://localhost:7357/assets/vendor.js:30) 
      at http://localhost:7357/assets/vendor.js:156 
      at http://localhost:7357/assets/test-loader.js:29 
      at http://localhost:7357/assets/test-loader.js:21 
      at http://localhost:7357/assets/test-loader.js:40 
      at http://localhost:7357/assets/test-support.js:13918 
    Log: > 
... 

有一兩件事我注意到的是,ember-sinon已在bower_components/目錄很輕的足跡:

$ls bower_components/sinon/ 
index.js 

我試過改變輸入行爲import { sinon } from 'sinon/index';,但那也沒有奏效。

任何幫助將不勝感激。我非常喜歡ember-cli,bower和es6模塊,因此對這些作品的背景鏈接將不勝感激。謝謝!

回答

1

好的,在RTFMing for litterally分鐘後,我在燼文檔的test assets部分找到了答案。

的設置:

ember install ember-sinon 

^所以我跑手發電機,這將拋出一個錯誤。

ember generate ember-sinon 

然後,在Brocfile進口興農:

var isProduction = EmberApp.env() === 'production'; 
if (!isProduction) { 
    app.import(app.bowerDirectory + '/sinon/index.js', {type: 'test'}); 
} 

現在,您可以使用您的規格sinon.stub/spy/mock,但你仍然會得到約興農一個jshint錯誤沒有被定義。我通過加入以下內容來解決這個問題:

/* global sinon */ 

到規範的頂部。看起來你應該能夠在jshintrc文件中全局聲明這個,但我還沒有想到。

+2

要擺脫jshint錯誤,您必須將「sinon」添加到「tests/.jshintrc」中的predef數組中。不是項目寬「.jshintrc」!應該看起來像{「predef」:[「sinon」]}; – phlppn

+0

感謝@phlppn,這正是jshint的問題。 – spinlock