Ionic App的Karma单元测试。错误:[ng:areq]参数' StockCtrl'不是一个功能,未定义

时间:2015-07-31 21:18:03

标签: javascript angularjs unit-testing ionic-framework

我在尝试对离子应用进行单元测试时遇到错误。我经历了之前提出的问题而没有为我提供的答案。我有什么想法我做错了吗?

错误我得到了:

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();
   });
});

我试过' module'而不是'angular.module'正如之前发布的一个问题中的建议,但最终会出现其他错误:

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

我非常感谢你的帮助。

另外,有没有人知道如何测试$资源作为下一步?

1 个答案:

答案 0 :(得分:1)

您肯定需要删除beforeEach(angular.module('Shop'))并将其替换为beforeEach(module('Shop'))。没有找到离子模块的问题可能是一些问题。我首先要确保Karma在运行测试时将正确的文件加载到浏览器中并且正在以正确的顺序执行。检查Karma.conf文件中的files属性是否引用了离子模块。