Karma错误参数不是函数,未定义

时间:2017-02-16 12:53:22

标签: angularjs karma-jasmine

我是angularjs中的业力和茉莉花测试新手。我正在使用angularjs 1.2.13版本。我想测试我的控制器" LandCtrl"。在我的控制器规范文件中

   describe('Users factory',function(){
    var LandCon;
     beforeEach(angular.mock.module('routerApp'));

  beforeEach(inject(function(_UserAuth_) {
    LandCon = _UserAuth_;
  }));



  var $controller,$cookies,$timeout,$state,$scope;
  var userauth,searchfood;//$scope, UserAuth, $cookies, $state, searchFood, $rootScope, $timeout

beforeEach(inject(function(_$controller_,_UserAuth_,_$cookies_,_$state_,_searchFood_,_$timeout_,_$rootScope_){
    scope = _$rootScope_.$new();
    $controller = _$controller_;
    cookies = _$cookies_;
    state = _$state_;
    userauth = _UserAuth_;
    serchfood = _searchFood_;
    timeout = _$timeout_;
    landCtrl = $controller('LandCtrl',{$scope:scope,UserAuth:userauth,$cookies:cookies,$state:state,serchFood:serchfood,$rootScope:_$rootScope_,$timeout:timeout}).on(error,function(er){console.log(er);});
}));

 it('should be defined', function(){

     expect(landCtrl).toBeDefined();
 });  
});

我的控制器文件看起来像

routerApp.controller('LandCtrl', ['$scope', 'UserAuth', '$cookies', '$state', 'searchFood', '$rootScope', '$timeout', function ($scope, UserAuth, $cookies, $state, searchFood, $rootScope, $timeout) {

            $scope.Islocation = "";
            $rootScope.showMessage = false;
            $scope.location = {};
            var d = new Date();
            var yy = d.getFullYear();
            var mm = d.getMonth() + 1;
            var dd = d.getDate();
            if (mm < 10) {
                mm = "0" + mm;
            }
            if (dd < 10) {
                dd = "0" + dd;
            }
            var defaultdate = yy + "-" + mm + "-" + dd;
            //var defaultdate = "2017-01-19";

            $scope.LandsessionStatus = function () {
                return UserAuth.sessionStatus();
            };

            $scope.LandsessionUser = function () {
                return UserAuth.sessionUser();
            };

            $scope.Fn_logout = function () {
                UserAuth.logout();

            };

            $scope.Fn_openmenuClick = function () {

                if (searchFood.SetCookieData($scope.location) === 1 && $scope.place != undefined && $scope.place != '') {
                    $state.go('home');
                } else {
                    $rootScope.showMessage = true;
                    $timeout(function () {
                        $rootScope.showMessage = false;
                    }, 5000);
                }

            };

            $scope.Fn_ChangePlace = function (latitude, longitude, countrycode) {
                $scope.location.latitude = latitude;
                $scope.location.longitude = longitude;
                $scope.location.countrycode = countrycode;
                $scope.location.place = $scope.place;
                searchFood.SetCookieData($scope.location);
            };

            $scope.initDeliveryLocation = function(){
                var location = searchFood.GetCookieData();
                $scope.place = location.place;
                $scope.location = location;
            };
          $scope.initDeliveryLocation();
        }
    ]);

on karma start,显示以下错误

Users factory
    ×should be defined
        Error: [ng:areq] Argument 'LandCtrl' is not a function, got undefined
        http://errors.angularjs.org/1.2.13/ng/areq?p0=LandCtrl&p1=not%20a%20function%2C%20got%20undefined
            at angular/resources/angularfile.js:78:12
            at assertArg (angular/resources/angularfile.js:1365:11)
            at assertArgFn (angular/resources/angularfile.js:1375:3)
            at angular/resources/angularfile.js:6845:9
            at Object.<anonymous> (angular/mainctest.spec.js:22:13)
            at Object.invoke (angular/resources/angularfile.js:3720:17)
            at Object.workFn (app/node_modules/angular-mocks.js:2120:20)
        Error: Declaration Location
            at window.inject.angular.mock.inject (app/node_modules/angular-mocks.js:2105:25)
            at Suite.<anonymous> (angular/mainctest.spec.js:14:12)
            at angular/mainctest.spec.js:1:1

        ReferenceError: landCtrl is not defined
            at Object.<anonymous> (angular/mainctest.spec.js:27:10)


Chrome 56.0.2924 (Windows 7 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.442 secs / 0.068 secs)

我怎样才能纠正我的问题。我知道这是一个提出问题的问题。但我没有摆脱它。请帮助我,并提前致谢。

0 个答案:

没有答案