【问题标题】:Jasmine tests not detecting jQuery internationalized constantsJasmine 测试未检测到 jQuery 国际化常量
【发布时间】:2017-09-20 16:52:32
【问题描述】:

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

似乎发生了什么:问题似乎是messages.properties 中的常量被设置在beforeAll(...)单元测试运行之后。我在jQuery.i18n.properties(...)callback 函数中的1 Hello World! 之前从单元测试中看到console.log(...)。如何强制测试等待 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 运行。我还在使用带有mvn jasmine:bdd 的maven 运行测试。

更多信息

主目录下有一个文件也调用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 所以代码应该无关紧要。我发布这个是为了有可能它可能很重要。

【问题讨论】:

    标签: javascript jquery internationalization jasmine


    【解决方案1】:

    问题是我设置了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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 2017-07-29
      • 1970-01-01
      相关资源
      最近更新 更多