【问题标题】:Error: [$injector:modulerr] angularjs 1.7 and jasmine错误:[$injector:modulerr] angularjs 1.7 和 jasmine
【发布时间】:2019-02-26 05:55:35
【问题描述】:

controller.js

  var testApp = angular.module('testApp', ['testModule']);

  angular.module('testModule')
    .component('testModule', {
      template: '<p> some things will be here</p>',
      controller: [
        '$scope', '$http', 
        function testController($scope, $http){
          $scope.z = 0;
          $scope.sum = function() {
            $scope.z = $scope.x + $scope.y;
          };
        }
      ]
    });

test.js

describe('testModule', function () {

    beforeEach(module('testApp'));

    var $controller;

    beforeEach(inject(function(_$controller_){
      $controller = _$controller_;
    }));

    describe('tests', function () {

          // directive test i




          // controller test
          it('1 + 2 should equal 3', function () {
              var $scope = {};
              var controller = $controller('testController', { $scope: $scope });
              $scope.x = 1;
              $scope.y = 2;
              $scope.sum();
              expect($scope.z).toBe(3);
          });   

          // service  test
          it('http get test', inject(function($http, $httpBackend){
            var $scope = {};
            var controller = $controller('testController', { $scope: $scope });
            $http.get('http://localhost/ibrahim')
              .success(function(data, status, headers, config){
                $scope.valid = true;
                $scope.response = data;
              })
              .error(function(data, status, headers, config){
                $scope.vaid = false;
              });

              $httpBackend
                .when('GET', 'http://localhost/ibrahim')
                .respond(200, {
                    name: 'ibrahim',
                    country: 'sweden'
                });

              $httpBackend.flush();

              expect($scope.valid).toBe(true);
              expect($scope.response).toEqual({
                name: 'ibrahim',
                country: 'sweden'
              });
          }));

      }
    );
  });

我收到了这个错误

   Error: [$injector:modulerr] http://errors.angularjs.org/1.4.0-rc.2/$injector/modulerr?p0=testApp&p1=Error%3A%20%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.4.0-rc.2%2F%24injector%2Fmodulerr%3Fp0%3DtestModule%26p1%3DError%253A%2520%255B%2524injector%253Anomod%255D%2520http%253A%252F%252Ferrors.angularjs.org%252F1.4.0-rc.2%252F%2524injector%252Fnomod%253Fp0%253DtestModule%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A6%253A421%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A23%253A384%250A%2520%2520%2520%2520at%2520a%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A23%253A25)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A23%253A268%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A37%253A202%250A%2520%2520%2520%2520at%2520n%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A7%253A322)%250A%2520%2520%2520%2520at%2520g%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A37%253A50)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A37%253A219%250A%2520%2520%2520%2520at%2520n%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A7%253A322)%250A%2520%2520%2520%2520at%2520g%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A37%253A50)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A6%3A421%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A37%3A476%0A%20%20%20%20at%20n%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A7%3A322)%0A%20%20%20%20at%20g%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A37%3A50)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A37%3A219%0A%20%20%20%20at%20n%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A7%3A322)%0A%20%20%20%20at%20g%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A37%3A50)%0A%20%20%20%20at%20Object.ab%20%5Bas%20injector%5D%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A40%3A332)%0A%20%20%20%20at%20UserContext.workFn%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular-mocks.js%3F71a3fefebedb617c8cd52b83f351d31a45417323%3A2409%3A56)%0A%20%20%20%20at%20QueueRunner.attempt%20(http%3A%2F%2Flocalhost%3A9876%2FC%3A%2FUsers%2Fibrahim.yazici%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F1f336da4025323945fc818c69bdc564a85081e4e%3A5462%3A44)
        at lib/angular.min.js:6:421
        at lib/angular.min.js:37:476
        at n (lib/angular.min.js:7:322)
        at g (lib/angular.min.js:37:50)
        at Object.ab [as injector] (lib/angular.min.js:40:332)
        at UserContext.workFn (lib/angular-mocks.js:2409:56)
        at <Jasmine>
    Error: [$injector:modulerr] http://errors.angularjs.org/1.4.0-rc.2/$injector/modulerr?p0=testApp&p1=Error%3A%20%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.4.0-rc.2%2F%24injector%2Fmodulerr%3Fp0%3DtestModule%26p1%3DError%253A%2520%255B%2524injector%253Anomod%255D%2520http%253A%252F%252Ferrors.angularjs.org%252F1.4.0-rc.2%252F%2524injector%252Fnomod%253Fp0%253DtestModule%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A6%253A421%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A23%253A384%250A%2520%2520%2520%2520at%2520a%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A23%253A25)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A23%253A268%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A37%253A202%250A%2520%2520%2520%2520at%2520n%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A7%253A322)%250A%2520%2520%2520%2520at%2520g%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A37%253A50)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A37%253A219%250A%2520%2520%2520%2520at%2520n%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A7%253A322)%250A%2520%2520%2520%2520at%2520g%2520(http%253A%252F%252Flocalhost%253A9876%252Fbase%252Flib%252Fangular.min.js%253F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%253A37%253A50)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A6%3A421%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A37%3A476%0A%20%20%20%20at%20n%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A7%3A322)%0A%20%20%20%20at%20g%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A37%3A50)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A37%3A219%0A%20%20%20%20at%20n%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A7%3A322)%0A%20%20%20%20at%20g%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A37%3A50)%0A%20%20%20%20at%20Object.ab%20%5Bas%20injector%5D%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular.min.js%3F1fd32b3190c79d3de8452d8f156b7ff0af9e644e%3A40%3A332)%0A%20%20%20%20at%20UserContext.workFn%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flib%2Fangular-mocks.js%3F71a3fefebedb617c8cd52b83f351d31a45417323%3A2409%3A56)%0A%20%20%20%20at%20QueueRunner.attempt%20(http%3A%2F%2Flocalhost%3A9876%2FC%3A%2FUsers%2Fibrahim.yazici%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F1f336da4025323945fc818c69bdc564a85081e4e%3A5462%3A44)
        at lib/angular.min.js:6:421
        at lib/angular.min.js:37:476
        at n (lib/angular.min.js:7:322)
        at g (lib/angular.min.js:37:50)
        at Object.ab [as injector] (lib/angular.min.js:40:332)
        at UserContext.workFn (lib/angular-mocks.js:2409:56)
        at <Jasmine>

Chrome 72.0.3626 (Windows 10.0.0) 错误 { "message": "afterAll 中抛出一个错误\nUncaught Error: [$injector:nomod] http://errors.angularjs.org/1.4.0-rc.2/$injector/nomod?p0=testModule", "str": "afterAll 中抛出一个错误\nUncaught Error: [$injector:nomod] http://errors.angularjs.org/1.4.0-rc.2/$injector/nomod?p0=testModule" }

如果我只是使用这个控制器代码,我没有错误

  angular.module('testApp', [])
  .controller('testController', function testController($scope, $http) {
    $scope.z = 0;
    $scope.sum = function() {
      $scope.z = $scope.x + $scope.y;
    };
  });

但我也想测试指令。我不明白没有模板我该怎么做。为什么我会收到此错误,我该如何解决?或者有没有办法在不编写模板的情况下测试指令?谢谢

【问题讨论】:

    标签: angularjs unit-testing jasmine karma-jasmine


    【解决方案1】:

    试试这个

     var myApp = angular.module('testModule', [])
    
     myApp.component('testModule', {
     template: '<p> some things will be here</p>',
     controller: [
        '$scope', '$http', 
        function testController($scope, $http){
          $scope.z = 0;
          $scope.sum = function() {
            $scope.z = $scope.x + $scope.y;
          };
        }
      ]
    });
    
    var testApp = angular.module('testApp', ['testModule']);
    

    对我有用

    【讨论】:

      【解决方案2】:

      我相信您需要像这样注入$compile 提供程序:

      var $compile, $controller;
      
      ...
      
      beforeEach(inject(function(_$compile_, _$controller_){
        $compile = _$compile_;
        $controller = _$controller_;
      }));
      
      // your test code
      

      查看Testing Directives segment of the official guide 了解更多信息。

      【讨论】:

      • 遗憾的是没有任何改变
      猜你喜欢
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      相关资源
      最近更新 更多