如何在 Ember 单元测试中使用库?
How do I make use of libraries in my Ember unit tests?
我试图在我的 Ember 单元测试中使用一个库 (chance.js),但我似乎无法在我的测试中导入它。
到目前为止,我已经尝试使用 NPM (with and without ember-browserify), Bower(尝试在 ember-cli-build.js
中调用 app.import
。
无论我做什么,我似乎都无法访问 chance
functions/module。
如有任何帮助,我们将不胜感激。最高分将授予任何可以向我指出在单元测试中使用 chance.js
的 Ember 项目的人。
如果可能的话,在您的回答中简要说明哪种依赖管理是合适的将非常有帮助。从我读到的内容来看,NPM 似乎是首选,因为 Ember 项目正在寻求远离 Bower,请参阅 here。
此外,如果有人知道任何有用的文章(最新的也将是一个加号),请随时在评论中提及它们。
谢谢!
编辑:
我相信我看到的一些问题与使用 'anonymous' AMD 模块的 Chance 有关,Ember 的加载器不支持该模块。我又玩了一会儿,但最终选择了 ember-faker addon as it is supported out of the box thanks to John Otander。
我会把这个问题留在这里,希望有人能发布一个很好的答案来澄清事情:)。
一般问题:
“... NPM 似乎是首选,因为 Ember 项目正在寻求远离 Bower...”。我也看了,得出了同样的结论。
"which dependency management is appropriate..." 我对一些模块(lodash、d3、supergroup、getstream、now chance)进行了练习,每次 ember-browserify
都运行良好。
关于 chance
:看起来它与 ember-browserify
搭配使用效果很好。
//console
npm install --save-dev ember-browserify
npm install --save-dev chance
//using in application controller
import Ember from 'ember';
import Ch from 'npm:chance';
var chance = new Ch();
export default Ember.Controller.extend({
chanceString: null,
chanceBool: chance.bool()
});
// using in application controller test
import Ch from 'npm:chance';
var chance = new Ch();
test('can use chance in tests', function(assert) {
var controller = this.subject();
controller.set('chanceString', chance.phone());
assert.ok(controller.get('chanceString'));
});
查看工作示例here
Ember CLI now supports Anonymous AMD modules。在您的 ember-cli-build.js
文件中,将 app.import()
与 AMD 转换一起使用,为匿名模块命名如下:
app.import('bower_components/chance/dist/chance.min.js', {
using: [
{ transformation: 'amd', as: 'chance' }
]
});
然后,您可以在整个应用程序中导入它并像这样进行测试:
import chance from 'chance';
我试图在我的 Ember 单元测试中使用一个库 (chance.js),但我似乎无法在我的测试中导入它。
到目前为止,我已经尝试使用 NPM (with and without ember-browserify), Bower(尝试在 ember-cli-build.js
中调用 app.import
。
无论我做什么,我似乎都无法访问 chance
functions/module。
如有任何帮助,我们将不胜感激。最高分将授予任何可以向我指出在单元测试中使用 chance.js
的 Ember 项目的人。
如果可能的话,在您的回答中简要说明哪种依赖管理是合适的将非常有帮助。从我读到的内容来看,NPM 似乎是首选,因为 Ember 项目正在寻求远离 Bower,请参阅 here。
此外,如果有人知道任何有用的文章(最新的也将是一个加号),请随时在评论中提及它们。
谢谢!
编辑: 我相信我看到的一些问题与使用 'anonymous' AMD 模块的 Chance 有关,Ember 的加载器不支持该模块。我又玩了一会儿,但最终选择了 ember-faker addon as it is supported out of the box thanks to John Otander。
我会把这个问题留在这里,希望有人能发布一个很好的答案来澄清事情:)。
一般问题:
“... NPM 似乎是首选,因为 Ember 项目正在寻求远离 Bower...”。我也看了,得出了同样的结论。
"which dependency management is appropriate..." 我对一些模块(lodash、d3、supergroup、getstream、now chance)进行了练习,每次
ember-browserify
都运行良好。
关于 chance
:看起来它与 ember-browserify
搭配使用效果很好。
//console
npm install --save-dev ember-browserify
npm install --save-dev chance
//using in application controller
import Ember from 'ember';
import Ch from 'npm:chance';
var chance = new Ch();
export default Ember.Controller.extend({
chanceString: null,
chanceBool: chance.bool()
});
// using in application controller test
import Ch from 'npm:chance';
var chance = new Ch();
test('can use chance in tests', function(assert) {
var controller = this.subject();
controller.set('chanceString', chance.phone());
assert.ok(controller.get('chanceString'));
});
查看工作示例here
Ember CLI now supports Anonymous AMD modules。在您的 ember-cli-build.js
文件中,将 app.import()
与 AMD 转换一起使用,为匿名模块命名如下:
app.import('bower_components/chance/dist/chance.min.js', {
using: [
{ transformation: 'amd', as: 'chance' }
]
});
然后,您可以在整个应用程序中导入它并像这样进行测试:
import chance from 'chance';