错误:[ng:areq] http://errors.angularjs.org/1.3.15/ng/areq?p0=MainContr oller& p1 = not%20a%20function%2C%20got%20undefined

时间:2015-09-02 13:41:33

标签: angularjs karma-jasmine

我已经为我的控制器创建了单元测试,但它的错误就像这样

  

错误:[ng:areq] http://errors.angularjs.org/1.3.15/ng/areq?p0=MainContr   奥勒&安培; P1 =未%20A%20function%2C%20got%20undefined

无法找到此问题。我已经附上了以下代码,我试过了

main.spc.js

describe("Unit: MainController", function() {

    beforeEach(module("hawkApp"));

    describe("MainController", function() {

        var $scope; 
        var $httpBackend;
        var ctrl;
        var rootScope;  

        beforeEach(inject(function ($injector, $route, $rootScope, $q, $compile, $controller, $window, $http, login_auth) { 

            $scope = $rootScope.$new();

            $httpBackend = $injector.get('$httpBackend');
            rootScope = $rootScope;

            ctrl = new $controller('MainController', { 
                $route : { current : { params : 38 } },
                $rootScope : $rootScope,
                $scope : $scope,
                // $resource : $resource,
                $http : $http,
                $window : $window,
                login_auth : login_auth,
                $q : $q,
                $compile : $compile,
                $routeParams : {
                    view : 'hawk_index',
                },
                $controller : $controller
            });

        }));

        afterEach(function () {
                $httpBackend.flush();
                $httpBackend.verifyNoOutstandingExpectation();
                $httpBackend.verifyNoOutstandingRequest();
        });

        it('should have a properly working Main controller', inject(function ($rootScope) {
            expect(ctrl);
        }));

    });

});

我的控制器

main.js

hawkApp.controller('MainController', function($scope,$window,$http,login_auth){

    /** show latest hawk**/ 
    $scope.scrolllatestnews = function(){
        // increase the scroll position by 3 px every 10th of a second
        function pausecomp(millis){
          var date = new Date();
          var curDate = null;
          do { curDate = new Date(); }
          while(curDate-date < millis);
        }   
        setInterval(function() { 
            // make sure it's not at the bottom
        var div = document.getElementById("scroll");
            if (div.scrollTop < div.scrollHeight - div.clientHeight)
                div.scrollTop += 3; // move down
        else {
            pausecomp(100); 
                div.scrollTop = 0; // move down
        }
        }, 500); // 100 milliseconds
    }
    /** show latest hawk**/


    $scope.toSection3 = function(data,data1) {
      if(!document.getElementById(data)){
         $window.location.href = "#/"+data1;
      }
    }

    //showpress_content
    $scope.showpress_content = function(){
        $scope.items = true;
    }

    //check login authentication
        $scope.username = "admin";
        $scope.password = "demo";
        login_auth.login_access($scope.username,$scope.password).success(function(response){
            console.log("login success");
            console.log(response);
            return response;
        }).error(function(){
            console.log("Post failed!");
            return "Post failed!";
        });

    //login validate
        login_auth.login_validate().success(function(response){
            console.log("validation success");
            console.log(response);
            return response;
        }).error(function(){
            console.log("Post failed!");
            return "Post failed!";
        });
    //logout 
        login_auth.login_logout().success(function(response){
            console.log("logout success");
            console.log(response);
            return response;
        }).error(function(){
            console.log("Post failed!");
            return "Post failed!";
        });
    //check login authentication

}).value('duScrollOffset', 30);

1 个答案:

答案 0 :(得分:0)

在查找您的控制器时不要使用new而无需传递$controller提供商:

           ctrl = $controller('MainController', { 
                $route : { current : { params : 38 } },
                $rootScope : $rootScope,
                $scope : $scope,
                // $resource : $resource,
                $http : $http,
                $window : $window,
                login_auth : login_auth,
                $q : $q,
                $compile : $compile,
                $routeParams : {
                    view : 'hawk_index',
                }
            });