Jasmine 测试不检测 jQuery 国际化常量



问题:我遇到一个问题,我尝试从.properties文件中读取常量,但它的变量不会在it单元测试的范围内定义。执行以下测试将打印1 Hello World!,但不会打印2 Hello World!。Jasmine说MY_CONSTANT不是在print something单元测试中定义的,但是它是在调用jQuery.i18n.properties(...)callback中定义的。

似乎正在发生的事情:问题似乎是在单元测试运行后messages.properties中的常量是在beforeAll(...)中设置的。我看到单元测试中的console.log(...)打印在jQuery.i18n.properties(...)callback函数的1 Hello World!之前。如何强制测试等待beforeAll(...)中的异步调用首先完成?

简化的Jasmine测试文件(src/test/javascript/mySpec.js):

describe('Test for buttons and output in home page.', function() {
beforeAll(function() {
jQuery.i18n.properties({
name : 'Messages',
path : 'src/main/resources/bundle/',
mode : 'both',
language : 'en_US',
async : true,
callback: function() {
console.log('1 ' + MY_CONSTANT);
}
});
});
beforeEach(function() {
jasmine.getFixtures().fixturesPath = 'src/test/javascript/fixture';
loadFixtures('myFixture.html');
$.holdReady(false);
});
it('print something.', function(){
console.log('2 ' + MY_CONSTANT);
});
});

简化的属性文件(src/main/resources/bundle/messages.properties):

MY_CONSTANT=Hello World!

P.S. 这是一个使用 Java 和 jQuery 的项目。Jasmine 单元测试目前纯粹使用 jQuery 和 Javascript 运行。我还使用 maven 和mvn jasmine:bdd运行测试。

更多信息

主目录中有一个文件也调用jQuery.i18n.propertiessrc/main/resources/assets/internationalization.js,用代码调用:

$(document).ready(function() {
jQuery.i18n.properties({
name: 'Messages',
path: 'bundle/',
mode: 'both',
language: 'en_US',
async: true,
callback: function() {
initializeText();
}
});
});

initializeText()基本上是一个函数,它通过将属性与 HTML ID 相关联来设置一些必要的显示文本,但它不会设置MY_CONSTANT因此代码无关紧要。我发布这个是为了有机会这可能很重要。

问题是我设置了async: true.当我删除async: true时,测试通过了:

beforeAll(function() {
jQuery.i18n.properties({
name : 'Messages',
path : 'src/main/resources/bundle/',
mode : 'both',
language : 'en_US',
callback: function() {
console.log('1 ' + MY_CONSTANT);
}
});
});

该文档实际上位于 github 存储库自述文件中: https://github.com/jquery-i18n-properties/jquery-i18n-properties

最新更新