首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在单元测试中访问Ember中的速记助手?

如何在单元测试中访问Ember中的速记助手?
EN

Stack Overflow用户
提问于 2015-06-17 10:37:44
回答 1查看 72关注 0票数 0

如何在单元测试中访问速记助手以进行测试?

示例:

/助理员/全地址

代码语言:javascript
复制
import Ember from 'ember';

export default Ember.Helper.helper(function(params, hash) {
  var fullAddress = hash.line1 === "" ? "" : hash.line1 + ", ";
  fullAddress += hash.town === "" ? "" : hash.town + ", " ;
  fullAddress += hash.postCode === "" ? "" : hash.postCode + ", ";

  if (fullAddress.length > 2) {
    fullAddress = fullAddress.replace(/,(\s+)?$/, "");
  }

  return fullAddress;
});

在addresses.hbs中使用速记助手

代码语言:javascript
复制
<h4>Addresses</h4>
{{#each model.addresses key="id" as |address|}}
  <p>
    {{full-address line1=address.line1 town=address.town postCode=address.postCode}}
  </p>
{{/each}}

全地址测试

代码语言:javascript
复制
import { fullAddress } from '../../../helpers/full-address';
import { module, test } from 'qunit';

module('Unit | Helper | full address');

// Replace this with your real tests.
test('it works', function(assert) {
  var line1 = "123 Test Street";
  var town = "My Town";
  var postCode = "TE5 5ST";

  var expected = line1 + ", " + town + ", " + postCode;

  var result = ??? // Call helper here
  assert.equal(result, expected);
});

我如何用散列的这三个变量调用速记助手?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-17 13:18:35

您需要稍微修改您的助手文件:

代码语言:javascript
复制
import Ember from 'ember';

export function fullAddress(params, hash) {
  let retValue = hash.line1 === "" ? "" : hash.line1 + ", ";
  retValue += hash.town === "" ? "" : hash.town + ", " ;
  retValue += hash.postCode === "" ? "" : hash.postCode + ", ";

  if (retValue.length > 2) {
    retValue = retValue.replace(/,(\s+)?$/, "");
  }

  return retValue;
}

export default Ember.Helper.helper(fullAddress);

然后,你可以写你的测试:

代码语言:javascript
复制
import { fullAddress } from '../../../helpers/full-address';
import { module, test } from 'qunit';

module('Unit | Helper | full address');

test('it works', function(assert) {
  var line1 = "123 Test Street";
  var town = "My Town";
  var postCode = "TE5 5ST";

  var expected = line1 + ", " + town + ", " + postCode;

  var result = fullAddress(null, {
    line1: line1,
    town: town,
    postCode: postCode
  }); // Call helper here

  assert.equal(result, expected);
});

如果您只运行这个测试模块,您将得到:

在这种情况下,fullAddress的返回值是:123 Test Street, My Town, TE5 5ST,它与期望值匹配。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30889106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档