繁体   English   中英

Ionic App的业力单元测试。 错误:[ng:areq]参数'StockCtrl'不是函数,未定义

[英]Karma Unit testing of Ionic App. Error: [ng:areq] Argument 'StockCtrl' is not a function, got undefined

尝试对离子应用程序进行单元测试时出现错误。 我经历了之前提出的问题,但没有答案对我有用。 有什么想法我做错了吗?

我得到的错误:

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

我已尝试使用“模块”而不是先前发布的问题之一中建议的“ 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

我将非常感谢您的帮助。

另外,有人知道下一步如何测试$ resource吗?

您绝对需要删除beforeEach(angular.module('Shop'))并将其替换为beforeEach(module('Shop')) 找不到离子模块的问题可能是几件事。 首先,请确保Karma在运行测试时以正确的顺序将正确的文件加载到浏览器中。 检查Karma.conf文件中的files属性是否已引用您的ionic模块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM