我针对使用 ECMAScript 国际化 API 的 AngularJs
服务运行了一系列 Jasmine
测试。当我通过 Chrome 运行它们时,它们都运行成功。但是,当我使用 PhantomJS
通过 Maven 运行它们时,它们都失败了,因为它似乎 PhantomJs
还不支持国际化 API。
我使用 Intl 对象进行测试时得到的错误消息是:
1: ReferenceError: Can't find variable: Intl in localizationService.js
其余的测试都失败了。
测试很简单,看起来像这样:
it('Format date with en-us locale', (function (){
var date= "06/13/2013"
expect(service.date(date,'en-us')).toEqual("6/13/2013");
}))
服务 (localizationService.js) 中的方法是 Intl API 的简单包装器:
function getCurrentTimeZone(){
return Intl.DateTimeFormat().resolved.timeZone
}
function date(dateInput,locale,options){
// some other stuff
// ...
if (locale) {
return _date.toLocaleDateString(locale,options);
} else {
return _date.toLocaleDateString();
}
}
我的问题是:
1- 我的假设是否正确,即 PhantomJS v1.9.2
不支持 ECMAScript 国际化 API
?有什么可以确认的吗?
2- 我该如何解决这个问题?我需要通过 Maven 运行我的测试,我将有更多测试以一种或另一种方式访问我的 localizationService API。
谢谢
最佳答案
不确定您是否在使用 Karma,但这是我必须做的来解决同样的问题。
npm install karma-intl-shim --save-dev
这还将安装 polyfill 库 Intl;
将“intl-shim”添加到 karma.conf.js 中的框架集合:
... frameworks: ['intl-shim'],
在 karma.conf.js 中添加您希望用于测试的语言环境文件,例如“en-US”:
... files: [ './node_modules/Intl/locale-data/jsonp/en-US.js', ...
关于PhantomJS 不支持 Javascript 国际化 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19228209/