I have code that works. But I have no idea how to test it. A FeeRule has many FeeParameters.
angular.module('feeSuitesApp')
.controller('RootCtrl', [
'FeeSuiteCrud',
'FeeSuite',
'FeeRule',
'FeeParameter',
'feeSuites',
'$scope',
'$q',
function(FeeSuiteCrud, FeeSuite, FeeRule, FeeParameter, feeSuites, $scope, $q){
$scope.feeSuite = feeSuites[0];
if($scope.feeSuite){
getFeeRules().then(function(response){
$scope.feeRules = response;
$scope.feeRules.forEach(function(feeRule){
getFeeParameters(feeRule).then(function(response){
feeRule.parameter_name = response.name
feeRule.parameter_type = response.parameter_type;
});
});
});
}
function getFeeRules(){
var feeCrud = new FeeSuiteCrud(FeeSuite, FeeRule);
return feeCrud.get($scope.feeSuite.id);
}
function getFeeParameters(feeRule){
var feeCrud = new FeeSuiteCrud(FeeParameter);
return feeCrud.get(feeRule.fee_parameter_id);
}
getFeeRules and getFeeParameters return promises from $http.get().
My test:
it("gets feeRules and their feeParameters", function(){
var JSONResponse = {"master":[{"id":30,"fee_suite_id":8,"fee_parameter_id":2,"name":"Discover Fee","multiplier":0.045,"addend":0.0,"order":1,"created_at":"2016-09-27T10:12:26.000-05:00","updated_at":"2016-09-27T10:12:26.000-05:00"}]};
var feeRule = new FeeRule({
id: 30,
name: 'Discover Fee',
fee_suite_id: 8,
fee_parameter_id: 2,
multiplier: 0.045,
addend: 0,
order: 1
});
var feeParameter = new FeeParameter({
id: 2,
fee_type_id: 1,
name: "Discover subtotal",
value: "discover",
parameter_type: "currency"
});
feeRule.parameter_name = feeParameter.name;
feeRule.parameter_type = feeParameter.parameter_type;
$httpBackend.expectGET('/api/v3/fee_suites/8?association=fee_rules').respond(JSONResponse);
JSONResponse = {"id":2,"fee_type_id":1,"name":"Discover subtotal","value":"discover","parameter_type":"currency","created_at":"2016-09-27T10:12:25.000-05:00","updated_at":"2016-09-27T10:12:25.000-05:00"};
$httpBackend.expectGET('/api/v3/fee_parameters/2').respond(JSONResponse);
$rootScope.$digest();
$httpBackend.flush();
expect(scope.feeRules).toEqual([feeRule]);
});
Doesn't seem the second promise gets resolved as I get the following error:
Expected [ FeeRule({ id: 30, name: 'Discover Fee', fee_suite_id: 8,
fee_parameter_id: 2, multiplier: 0.045, addend: 0, order: 1 }) ] to equal [
FeeRule({ id: 30, name: 'Discover Fee', fee_suite_id: 8, fee_parameter_id: 2,
multiplier: 0.045, addend: 0, order: 1, parameter_name: 'Discover subtotal',
parameter_type: 'currency' }) ].
use it twice it will work
$httpBackend.flush();
$httpBackend.flush();
and instead of expect get use when('GET',URL)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.