Reputation: 43
I am testing angular 1.3 (coffeescript) with ui-router 0.2.15 and jasmine 1.3
I have my module set up as follows
angular.module('myModule', [
'ui.router'
])
.config ($stateProvider) ->
$stateProvider.state 'dashboard.settings.account',
url: '/account'
controller: 'SettingsAccountController'
templateUrl: 'dashboard/settings/account/account.tpl.html'
resolve:
phones: (Resolver) ->
return Resolver.resolvePhones()
The question is: how to test resolve function within the state? I've mocked the resolve service in controller so other tests work fine, but I don't know how to get to this particular function.
Upvotes: 1
Views: 863
Reputation: 186
Not aware of CoffeeScript syntax, but this is how I test my simple route configuration in regular JS.
describe('ui-router config', function() {
var $rootScope, $state, $injector, myServiceMock, state = 'dashboard.settings.account';
beforeEach(function() {
module('myModule', function($provide) {
$provide.value('Resolver', myServiceMock = {});
});
inject(function(_$rootScope_, _$state_, _$injector_, $templateCache) {
$rootScope = _$rootScope_;
$state = _$state_;
$injector = _$injector_;
$templateCache.put('template.html', '');//empty html content
})
});
it('should respond to URL', function() {
expect($state.href(state)).toEqual('#/state');
});
it('should resolve data', function() {
myServiceMock.resolvePhones= jasmine.createSpy('resolvePhones').andReturn('findAll');
// for jasmine version greater than 2.0, replace "andReturn" with "and.returnValue"
$state.go(state);
$rootScope.$digest();
expect($state.current.name).toBe(state);
// Call invoke to inject dependencies and run function
expect($injector.invoke($state.current.resolve.data)).toBe('findAll');
});
});
Its my first time testing route configuration as well. This works fine if your service does not return promise. I'm facing issues when the service method returns promise. Let me know if you can get it working with service returning promise.
Upvotes: 0
Reputation: 4578
you just need to inject your service
which is being resolved before loading the controller
.
var phones;
beforeEach(inject(function(Resolver){
phones = Resolver.resolvePhones()
}));
now inject this phones
in you controller .
Upvotes: 0