gpt4 book ai didi

javascript - 如何从 Jasmine 测试用例中解析 Angular lang json 文件( Angular 本地化模块)

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:40:27 24 4
gpt4 key购买 nike

我有使用 Angular 本地化语言环境服务的 Angular 服务方法。我想为方法编写 Jasmine 测试用例,但由于 Jasmine 无法做到而失败解决它。可能不是等到完全解决

//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')
})
})

如何解决上述问题?

最佳答案

您似乎遇到了竞争条件(文档摘录):

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. Failure to do so may cause unexpected (empty) results due to the asynchronous nature of your entire JavaScript application.

You may use locale.getPath() to find the filename for any given token.

因此解决方案应该是注入(inject) 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);
});

为了安全起见,您可能还应该在服务本身中执行相同的操作。

关于javascript - 如何从 Jasmine 测试用例中解析 Angular lang json 文件( Angular 本地化模块),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42977987/

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