Voglio testare questo controllerCome iniettare il valore angolare e la costante angolare nel test dell'unità di karma?
/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];
};
...
}
]);
ho cambiato il controller da quando ho scritto i test. Alcune costanti si sono trasferiti a angular.module('c2gyoApp').value('smConfig'){}
e ho anche bisogno della costante da angular.module('c2gyoApp').constant('stadtmobilRates'){}
:
/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
},
...
});
Questa è la mia prova finora:
/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));
...
});
});
Come si iniettano angular.module('c2gyoApp').value('smConfig'){}
e angular.module('c2gyoApp').constant('stadtmobilRates'){}
nel test? Sto usando il layout standard degli yeoman. Il file karma.conf include tutti i file .js necessari, quindi è solo una questione di dove iniettare gli elementi angolari.