简体   繁体   English

如何从jasmine测试用例解析角度lang json文件(角度定位模块)

[英]How to resolve angular lang json file(angular-localization module) from jasmine test case

I have angular service method which uses angular-localization locale service . 我有角度服务方法,它使用角度本地化的区域设置服务。 I want to write jasmine test cases for method , which are failing as jasmine is not able to resolve it. 我想为方法编写jasmine测试用例,因为jasmine无法解决它而失败。 May be it is not waiting till it resolve completely 也许它不会等到它彻底解决

//service method
$scope.getExcelName = function() {
  var name = locale.getString('export.fileName')
  return name;
}

//lang file 
'fileName': 'Status Report'

//Jasmine test case
describe('Service:MyService', function() {
  var myService
  beforeEach(module('app'))

  beforeEach(inject(function($injector) {
    myService = $injector.get('MyService');
  }))

  it('check export name', function() {
    //myService.getExcel giving '' instead of  Status Report
    expect(myService.getExcelName()).toBe('Status Report')
  })
})

How to resolve above issue ? 如何解决上述问题?

It seems like you are experiencing a race condition (excerpt from the documentation): 您似乎遇到了竞争条件(摘自文档):

Race conditions 比赛条件

Always use locale.ready() passing in the filename(s) in which the token(s) exists in that context, and enclose any other relevant calls to locale.getString() within this promise's callback function block. 始终使用locale.ready()传入该上下文中存在令牌的文件名,并在此promise的回调函数块中包含对locale.getString()的任何其他相关调用。 Failure to do so may cause unexpected (empty) results due to the asynchronous nature of your entire JavaScript application. 由于整个JavaScript应用程序的异步性质,如果不这样做可能会导致意外(空)结果。

You may use locale.getPath() to find the filename for any given token. 您可以使用locale.getPath()来查找任何给定标记的文件名。

So the solution should be to inject the locale service and wrap the expect line inside a locale.ready() call, eg: 所以解决方案应该是注入locale服务并将expect行包装在locale.ready()调用中,例如:

it('check export name', function(done) {
  locale.ready('export')
    .then(function() {
      expect(myService.getExcelName()).toBe('Status Report');
      done();
    })
    .catch(done.fail);
});

Probably you should also do the same in the service itself to be safe. 也许你应该在服务本身做同样的事情来保证安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM