I'm getting an error while trying to unit test my ionic app. I went through previously asked questions and non of the answers worked for me. Any ideas what I'm doing wrong?
Error I'm getting:
PhantomJS 1.9.8 (Mac OS X 0.0.0) StockCtrl should have a scope variable defined FAILED
TypeError: 'undefined' is not a function (evaluating 'queueableFn.fn.call(self.userContext)')
Error: [ng:areq] Argument 'StockCtrl' is not a function, got undefined
http://errors.angularjs.org/1.4.3/ng/areq?p0=StockCtrl&p1=not%20a%20function%2C%20got%20undefined
undefined
at assertArg (/Users/Projects/online-shop/www/lib/angular/angular.js:1770)
at assertArgFn (/Users/Projects/online-shop/www/lib/angular/angular.js:1781)
at /Users/Projects/online-shop/www/lib/angular/angular.js:8975
at /Users/Projects/online-shop/www/lib/angular-mocks/angular-mocks.js:1848
at /Users/Projects/online-shop/test/stockController.spec.js:9
at invoke (/Users/Projects/online-shop/www/lib/angular/angular.js:4450)
at workFn (/Users/Projects/online-shop/www/lib/angular-mocks/angular-mocks.js:2404)
PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.006 secs / 0.007 secs)
stockFactory.js
app.factory('stockService', function($resource){
return $resource('js/shopStock.json/:items', 'items');
})
stockController.js
app.controller("StockCtrl", function($scope, $rootScope, $stateParams, stockService) {
var items = stockService.get(function(){
$scope.stock = items['items'];
});
});
stockController.spec.js
describe('StockCtrl', function() {
beforeEach(angular.module('Shop'));
var scope;
beforeEach(inject(function($rootScope, $controller){
scope = $rootScope.$new();
$controller("StockCtrl", {$scope: scope});
}));
it("should have a scope variable defined", function() {
expect(scope).toBeDefined();
});
});
I have tried'module' instead of 'angular.module' as suggested in one of the previously posted questions but end up with other error:
Error: [$injector:modulerr] Failed to instantiate module Shop due to:
Error: [$injector:modulerr] Failed to instantiate module ionic due to:
Error: [$injector:nomod] Module 'ionic' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.4.3/$injector/nomod?p0=ionic
I would be very grateful for a help.
Also, does anyone know how test $resource as a next step?
You definitely need to remove beforeEach(angular.module('Shop'))
and replace it with beforeEach(module('Shop'))
. Your problem with the ionic module not being found could be a few things . I'd start by making sure Karma is loading the correct files into the browser when it runs your tests and that it's doing it in the right order. Check the files property in your Karma.conf file has your ionic module referenced.