如何在PhantomJS中测试String.prototype.includes [英] How to test String.prototype.includes in PhantomJS

查看:102
本文介绍了如何在PhantomJS中测试String.prototype.includes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ember-cli 0.2.7 使用 Ember.js 1.12.0 应用程序看起来像这样的代码:

I have an ember-cli 0.2.7 using Ember.js 1.12.0 app with a piece of code that looks like:

controllers / cart.js

import Ember from 'ember';

export default Ember.Controller.extend({

    footwearInCart: Ember.computed('model.@each.category', function() {
        return this.get('model').any(product => product.get('category').includes('Footwear'));
    })
});

它遍历模型中的所有对象,如果其类别属性具有鞋类,则返回true它。

It goes through all the objects in the model and returns true if their category property has 'footwear' in it.

我正试图这样测试:

tests / unit / controllers / cart-test.js

import { moduleFor, test } from 'ember-qunit';
import Ember from 'ember';

var products = [Ember.Object.create({name: 'shoe', category: 'Footwear', subTotal: 10}), Ember.Object.create({name: 'shirt', subTotal: 20})];

var model = Ember.ArrayProxy.create({
  content: Ember.A(products)
});

moduleFor('controller:cart', {
  beforeEach() {
    this.controller = this.subject();
  }
});

test('footwearInCart property works', function(assert) {
  this.controller.set('model', model);

  assert.equal(this.controller.get('footwearInCart'), true, 'The footwearInCart function returns true if the category property of product in cart contains the word "Footwear"');
});

代码按照运行应用程序时的方式工作,但 PhantomJS 显然无法识别.includes方法。 (此方法记录在此处 String.prototype.includes ()

The code works the way it should when I run the app, but PhantomJS apparently does not recognise the .includes method. (The method is documented here String.prototype.includes()

如何让PhantomJS识别.includes方法?

How can I get PhantomJS to recognize the .includes method?

谢谢!

推荐答案

显然PhantomJS没有正确实现ES6功能。幸运的是 String.prototype.includes 非常容易填充。您可以选择是否要在测试套件或控制器中执行此操作。填充代码为:

Apparently PhantomJS doesn't implement ES6 features properly. Luckily String.prototype.includes is quite easy to polyfill. You can choose if you want to do that in the test suite or the controller. The polyfill code is:

if (!String.prototype.includes) {
  String.prototype.includes = function() {'use strict';
    return String.prototype.indexOf.apply(this, arguments) !== -1;
  };
}

要么放它就在断言调用之前(你可能想要使用一个标记来记住你添加了polyfill并在 assert )或d o它在模块本身中,在 export 块之前或之后。

Either put it right before the assert call (you might want to use a flag to remember of you added the polyfill and remove it after the assert), or do it in the module itself, before or after the export block.

这篇关于如何在PhantomJS中测试String.prototype.includes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆