在EmberJS中对组件进行单元测试的正确方法

Huafu

我正在尝试在我的Ember应用程序单元测试中测试组件,直到现在为止一切都很好,除了我处于断言需要呈现其模板的时候。

通常这样做会打电话给

var comp = App.SomeNamedComponent.create();
var comp.appendTo(App.rootElement);

但是,尽管这确实创建了组件的基础元素,但并未呈现其模板。经过一些研究,我最终发现该组件上templateName没有template设置属性,也没有设置属性。因此,我决定templateName自己动手,但后来抱怨了A Component must have a parent view in order to yield.

然后,我决定使用该组件的模板在测试中创建另一个自定义视图,但随后我无法访问该组件的实例...

我需要访问该实例以进行断言,并且需要渲染它的模板,因为某些属性是根据模板中某些元素的css计算出来的。

罗伯特·杰克逊

这是我通常在不需要容器时测试组件的方式(特别是在以编程方式向组件提供模板和布局时):

Ember.testing = true;

MyAwesomeComponent = Ember.Component.extend();

function createComponent(componentName, factory, options) {
  if (typeof options.template === 'string') {
    options.template = Ember.Handlebars.compile(options.template);
  }

  if (typeof options.layout === 'string') {
    options.layout = Ember.Handlebars.compile(options.layout);
  }

  if (options.template && !options.layout) {
    options.layout = options.template;
    delete options.template;
  }

  var component = factory.create(options);

  Ember.run(function(){
    component.appendTo('#qunit-fixture');
  });

  return component;
}

module('component testing sample');

test('a component with template', function(){
  var options = {layout: 'woot woot{{fullName}}'};

  var component = createComponent('my-awesome', MyAwesomeComponent, options);

  equal(component.$().text(), 'woot woot');
});

test('a component with custom options and a template', function(){
  var options = {
    fullName: 'Robert Jackson',
    layout: '{{fullName}}'
  };

  var component = createComponent('my-awesome', MyAwesomeComponent, options);

  equal(component.$().text(), 'Robert Jackson');
});

参见示例JSBin


如果您需要/希望能够查找模板,则可以使用类似以下的内容(它创建一个隔离的容器):

Ember.testing = true;

MyAwesomeComponent = Ember.Component.extend();

function isolatedContainer() {
  var container = new Ember.Container();

  container.optionsForType('component', { singleton: false });
  container.optionsForType('view', { singleton: false });
  container.optionsForType('template', { instantiate: false });
  container.optionsForType('helper', { instantiate: false });

  return container;
}

function createComponent(componentName, factory, options) {
  var fullName = 'component:' + componentName,
      templateFullName = 'template:components/' + componentName;

  container.register(fullName, factory);

  if (container.has(templateFullName)) {
    container.injection(fullName, 'layout', templateFullName);
  }

  var Component = container.lookupFactory(fullName),
      component = Component.create(options);

  Ember.run(function(){
    component.appendTo('#qunit-fixture');
  });

  return component;
}

function registerTemplate(name, template){
  if (typeof template !== 'function') {
    template = Ember.Handlebars.compile(template);
  }

  container.register('template:' + name, template);
}

var container;

module('component testing sample', {
  setup: function(){
    container = isolatedContainer();
  },
  teardown: function(){
    Ember.run(container, 'destroy');
  }
});

test('a component with template', function(){
  registerTemplate('components/my-awesome', 'woot woot{{fullName}}');

  var component = createComponent('my-awesome', MyAwesomeComponent);

  equal(component.$().text(), 'woot woot');
});

test('a component with custom options and a template', function(){
  registerTemplate('components/my-awesome', '{{fullName}}');

  var component = createComponent('my-awesome', MyAwesomeComponent, {fullName: 'Robert Jackson'});

  equal(component.$().text(), 'Robert Jackson');
});

容器版本的JSBin

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在EmberJS中对组件进行单元测试的正确方法

来自分类Dev

对EmberJS App进行单元测试(实际单元测试)

来自分类Dev

如何正确地对React组件进行单元测试?

来自分类Dev

如何对依赖服务的组件的方法进行单元测试?

来自分类Dev

单元测试; 正确的方法?

来自分类Dev

正确的单元测试方法

来自分类Dev

正确的单元测试方法

来自分类Dev

使用dict进行单元测试的正确方法__repr__

来自分类Dev

如何对MatDialog(角材料)进行单元测试的正确方法?

来自分类Dev

什么是对python类进行单元测试的正确方法?

来自分类Dev

对基类中的受保护属性进行单元测试的正确方法

来自分类Dev

对Auth组件进行单元测试

来自分类Dev

如何正确进行单元测试

来自分类Dev

使用PostSharp进行正确的单元测试

来自分类Dev

在单元测试中模拟AngularJS服务的正确方法?

来自分类Dev

在OCaml中为模块编写单元测试的正确方法

来自分类Dev

在Clojure中运行单元测试的“正确”方法

来自分类Dev

在模块中编写单元测试的正确方法?

来自分类Dev

对类别中的私有方法进行单元测试?

来自分类Dev

在Akka中对私有方法进行单元测试

来自分类Dev

模拟服务中的方法进行单元测试

来自分类Dev

如何在模拟方法中对函数进行单元测试

来自分类Dev

对Angular中的Observable订阅的Service方法进行单元测试

来自分类Dev

如何使用 Jest 对连接的 Redux 组件内的组件中的 Redux 操作进行单元测试

来自分类Dev

Glimmer中的组件单元测试

来自分类Dev

单元测试组件

来自分类Dev

Android单元测试的正确方法

来自分类Dev

moq进行单元测试的通用方法

来自分类Dev

如何对POST方法进行单元测试

Related 相关文章

热门标签

归档