gpt4 book ai didi

javascript - Jasmine 测试未检测到 jQuery 国际化常量

转载 作者:行者123 更新时间:2023-11-28 04:10:27 26 4
gpt4 key购买 nike

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

似乎发生了什么:问题似乎是 messages.properties 中的常量正在设置为 beforeAll(...) 单元测试运行之后。我看到console.log(...)来自单元测试在 1 Hello World! 之前打印在jQuery.i18n.properties(...)callback功能。如何强制测试等待 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!

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

更多信息:

主目录中有一个文件也调用 jQuery.i18n.properties , src/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

关于javascript - Jasmine 测试未检测到 jQuery 国际化常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46327731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com