I'm using [email protected]
and resolve some resource at the route level for one of my component. The component works as expected but my test fails now.
Error
PhantomJS 2.1.1 (Linux 0.0.0) module ag.expense-claim ExpenseClaimController: should load data FAILED
TypeError: undefined is not an object (evaluating 'vm.attachedReceipts.results') (line 16)
ExpenseClaimViewController@app/expenses/expense_claim.js:16:50
ExpenseClaimViewController@[native code]
instantiate@node_modules/angular/angular.js:4733:61
$controller@node_modules/angular/angular.js:10369:39
node_modules/angular-mocks/angular-mocks.js:2221:21
$componentController@node_modules/angular-mocks/angular-mocks.js:2264:25
test/expenseSpec.js:18:40
invoke@node_modules/angular/angular.js:4718:24
workFn@node_modules/angular-mocks/angular-mocks.js:3085:26
loaded@http://localhost:9876/context.js:151:17
inject@node_modules/angular-mocks/angular-mocks.js:3051:28
test/expenseSpec.js:14:26
test/expenseSpec.js:11:13
global code@test/expenseSpec.js:1:9
Error: No pending request to flush ! in node_modules/angular-mocks/angular-mocks.js (line 1799)
flush@node_modules/angular-mocks/angular-mocks.js:1799:76
test/expenseSpec.js:53:31
loaded@http://localhost:9876/context.js:151:17
PhantomJS 2.1.1 (Linux 0.0.0): Executed 43 of 43 (1 FAILED) (0.257 secs / 0.387 secs)
error Command failed with exit code 1.
Test
describe('ExpenseClaimController:', function () {
var $scope, ctrl, attachedReceipts;
beforeEach(inject(function ($rootScope, $componentController, $stateParams) {
var attachedReceipts = {results: [{}]};
$scope = $rootScope.$new();
$stateParams.expenseClaimId = 1;
ctrl = $componentController('expenseClaim', {
$scope: $scope,
attachedReceipts: attachedReceipts
});
}));
it('should load data', function () {
…
});
Component
angular.module('ag.expenses')
.component('expenseClaim', {
templateUrl: '…',
controller: ExpenseClaimViewController,
controllerAs: 'vm',
bindings: {
attachedReceipts: "<"
}
});
function ExpenseClaimViewController($stateParams, $uibModal, API, gettextCatalog, alert) {
var vm = this;
vm.attachedReceipts = vm.attachedReceipts.results;
…
}
Route
.state('expense-claim', {
url: '/home_expense_report/:expenseClaimId',
template: '<expense-claim attached-receipts="$resolve.attachedReceipts"></expense-claim>',
resolve: {
attachedReceipts: function (API, $stateParams) {
return API.TransportCostAttachment.query({expenseClaimId: $stateParams.expenseClaimId}).$promise;
}
}
})
Question
I implement my solution based on How can I mock ui-router's resolve values when testing a state's configuration? but still can't get it to works. What am I missing?