javascript 如何将角度值和角度常数注入业力单元测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28432959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to inject angular value and angular constant into karma unit test?
提问by mles
I want to test this controller
我想测试这个控制器
/controllers/datetimepicker.js
/controllers/datetimepicker.js
angular.module('c2gyoApp')
.value('smConfig', {
rate: 'A',
tariff: 'classic'
})
.controller('DatetimepickerCtrl', [
'$scope',
'stadtmobilRates',
'smConfig',
function($scope, stadtmobilRates, smConfig) {
...
$scope.getCurrentRate = function(rate, tariff) {
// studi and classic have the same rates
if (tariff === 'studi') {
tariff = 'classic';
}
return stadtmobilRates[tariff][rate];
};
...
}
]);
I have changed the controller since I wrote the tests. Some constants have moved to angular.module('c2gyoApp').value('smConfig'){}
and I also need the constant from angular.module('c2gyoApp').constant('stadtmobilRates'){}
:
自从我编写测试以来,我已经更改了控制器。一些常量已移至angular.module('c2gyoApp').value('smConfig'){}
,我还需要来自angular.module('c2gyoApp').constant('stadtmobilRates'){}
以下内容的常量:
/services/stadtmobilrates.js
/services/stadtmobilrates.js
angular.module('c2gyoApp')
.constant('stadtmobilRates', {
'classic': {
'A': {
'night': 0,
'hour': 1.4,
'day': 21,
'week': 125,
'km000': 0.2,
'km101': 0.18,
'km701': 0.18
},
...
});
This is my test so far:
到目前为止,这是我的测试:
/test/spec/controllers/datetimepicker.js
/test/spec/controllers/datetimepicker.js
describe('Controller: DatetimepickerCtrl', function() {
// load the controller's module
beforeEach(module('c2gyoApp'));
var DatetimepickerCtrl;
var scope;
// Initialize the controller and a mock scope
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
DatetimepickerCtrl = $controller('DatetimepickerCtrl', {
$scope: scope
});
}));
it('should calculate the correct price', function() {
expect(scope.price(10, 10, 0, 0, 'A', 'basic')
.toFixed(2)).toEqual((18.20).toFixed(2));
...
});
});
How do I inject angular.module('c2gyoApp').value('smConfig'){}
and angular.module('c2gyoApp').constant('stadtmobilRates'){}
into the test? I'm using the standard yeoman layout. The karma.conf file includes all necessary .js files, so it's just a question of where to inject the angular elements.
我如何注入angular.module('c2gyoApp').value('smConfig'){}
和angular.module('c2gyoApp').constant('stadtmobilRates'){}
进入测试?我正在使用标准的自耕农布局。karma.conf 文件包括所有必需的 .js 文件,所以这只是从何处注入角度元素的问题。
回答by Davin Tryon
Since you are adding the c2gyoApp
module with:
由于您正在添加c2gyoApp
模块:
beforeEach(module('c2gyoApp'));
Everything registered inside that module should be injectable. So, this should work:
在该模块内注册的所有内容都应该是可注入的。所以,这应该有效:
var smConfig, stadtmobilRates;
beforeEach(inject(function($controller, $rootScope, _smConfig_, _stadtmobilRates_) {
scope = $rootScope.$new();
DatetimepickerCtrl = $controller('DatetimepickerCtrl', {
$scope: scope
});
smConfig = _smConfig_;
stadtmobilRates = _stadtmobilRates_;
}