如何将 sinon 导入 ember-cli 测试?
How can I import sinon into an ember-cli test?
我正在尝试在我的 ember-cli 应用程序中使用 sinon,但我不知道如何导入它。我在规范中有一个导入语句:
import { sinon } from 'sinon';
然后我尝试在测试中使用 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 模块的新手,因此非常感谢链接到这些作品的背景。谢谢!
好的,经过 RTFMing 几分钟后,我在 ember-cli 文档的 test assets 部分找到了答案。
设置:
ember install ember-sinon
^ 这会引发错误,所以我 运行 手动生成器。
ember generate ember-sinon
然后,在Brocfile
中导入sinon:
var isProduction = EmberApp.env() === 'production';
if (!isProduction) {
app.import(app.bowerDirectory + '/sinon/index.js', {type: 'test'});
}
现在,您可以在规范中使用 sinon.stub/spy/mock
,但您仍然会收到有关未定义 sinon 的 jshint 错误。我通过添加解决了这个问题:
/* global sinon */
到规范的顶部。看起来你应该能够在 jshintrc 文件中全局声明它,但我还没有弄清楚。
我正在尝试在我的 ember-cli 应用程序中使用 sinon,但我不知道如何导入它。我在规范中有一个导入语句:
import { sinon } from 'sinon';
然后我尝试在测试中使用 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 模块的新手,因此非常感谢链接到这些作品的背景。谢谢!
好的,经过 RTFMing 几分钟后,我在 ember-cli 文档的 test assets 部分找到了答案。
设置:
ember install ember-sinon
^ 这会引发错误,所以我 运行 手动生成器。
ember generate ember-sinon
然后,在Brocfile
中导入sinon:
var isProduction = EmberApp.env() === 'production';
if (!isProduction) {
app.import(app.bowerDirectory + '/sinon/index.js', {type: 'test'});
}
现在,您可以在规范中使用 sinon.stub/spy/mock
,但您仍然会收到有关未定义 sinon 的 jshint 错误。我通过添加解决了这个问题:
/* global sinon */
到规范的顶部。看起来你应该能够在 jshintrc 文件中全局声明它,但我还没有弄清楚。