Reputation: 98
When I run my Karma tests the following error is displayed:
Error: [$injector:unpr] Unknown provider: cartProvider <- cart <- CartCtrl
Here is my code:
The value cart
is resolved here:
angular.module 'myApp'
.config ($stateProvider) ->
$stateProvider.state 'cart',
url: '/cart'
resolve:
cart: (Cart) ->
Cart.getProducts()
templateUrl: 'app/cart/cart.html'
controller: 'CartCtrl'
controllerAs: 'cart'
The controller is here:
angular.module 'myApp'
.controller 'CartCtrl', (Cart, cart) ->
this.cart = cart
this.removeFromCart = (id) ->
Cart.removeProduct id
return this
And I guess i need to inject that resolved cart
on the controller here, but I don't know how:
describe 'Controller: CartCtrl', ->
# load the controller's module
beforeEach module 'myApp'
CartCtrl = undefined
scope = undefined
# Initialize the controller and a mock scope
beforeEach inject ($controller, $rootScope) ->
scope = $rootScope.$new()
CartCtrl = $controller 'CartCtrl',
$scope: scope
it 'should ...', ->
expect(1).toEqual 1
Upvotes: 2
Views: 1422
Reputation: 336
You need to declare an inline controller in your state definition to have your resolved cart
available to the injector. for example:
$stateProvider.state 'cart',
url: '/cart'
resolve:
cart: (Cart) ->
Cart.getProducts()
templateUrl: 'app/cart/cart.html'
controller: (cart, Cart) ->
this.cart = cart
this.removeFromCart = (id) ->
Cart.removeProduct id
return this
controllerAs: 'cart'
I know thats not really ideal, but thats been my experience.
Upvotes: 0
Reputation: 164731
You need to keep in mind that when testing your controller, the myApp
config block does not come into play.
You simply need to provide a value for the "cart" and "Cart" injectables, eg
var cart, Cart, scope, CartCtrl;
beforeEach(function() {
var cart = ['some', 'mocked', 'products'],
Cart = jasmine.createSypObj('Cart', ['removeProduct']);
module('myApp', function($provide) {
$provide.value('cart', cart);
$provide.value('Cart', Cart);
})
inject(function($controller, ...) {
// and so on
});
});
Apologies for the straight JS, I could never get into CoffeeScript
Upvotes: 3