javascript - 为 Angular 设置 Jasmine/karma

标签 javascript angularjs jasmine karma-runner

我正在轻松地遵循本指南 - http://paislee.io/testing-angularjs-with-grunt-karma-and-jasmine/ - 并有以下一些问题:

Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:modulerr] Failed to instantiate module ngRoute due to:
Error: [$injector:nomod] Module 'ngRoute' 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.

我按照它告诉我的那样安装了所有内容,并找到了另一个非常基本的设置测试的示例(这是我第一次实现,所以我从小处开始),测试看起来像这样

describe('Unit: MainCtrl', function() {
// Load the module with MainCtrl
beforeEach(module('myApp'));

var ctrl, scope;
// inject the $controller and $rootScope services
// in the beforeEach block
beforeEach(inject(function($controller, $rootScope) {
// Create a new scope that's a child of the $rootScope
scope = $rootScope.$new();
// Create the controller
ctrl = $controller('MainCtrl', {
  $scope: scope
});
}));

it('should create $scope.greeting when calling sayHello', 
function() {
  expect(scope.greeting).toBeUndefined();
  scope.sayHello();
  expect(scope.greeting).toEqual("Hello Ari");
 });

})

在 Controller 中它只是

 $scope.name = "Ari";
$scope.sayHello = function() {
$scope.greeting = "Hello " + $scope.name;
}

(来自 http://www.ng-newsletter.com/advent2013/#!/day/19 )

我使用常规的 ng-route 结构将应用程序和 Controller 设置在单独的文件夹中,我想这可能是问题所在?我为此使用了 grunt karma - 这是任务,以防万一它有帮助。

  karma: {  
      unit: {
        options: {
          frameworks: ['jasmine'],
          singleRun: true,
          browsers: ['PhantomJS'],
          files: [
            'app/bower_components/angular/angular.js',
            'app/bower_components/angular-mocks/angular-mocks.js',
            'app/scripts/**/*.js'
          ]
        }
      }
    }

我需要一些帮助,这是我第一次尝试这样做,我希望进行一些自动化测试。感谢您的阅读!!

最佳答案

您需要将 ngRoute 包含在 karma conf 的文件列表中。错误消息说明了这一点。

karma: {  
      unit: {
        options: {
          frameworks: ['jasmine'],
          singleRun: true,
          browsers: ['PhantomJS'],
          files: [
            'app/bower_components/angular/angular.js',
            'app/bower_components/angular-mocks/angular-mocks.js',
            'app/bower_components/angular-mocks/angular-route.js',
            'app/scripts/**/*.js'
          ]
        }
      }
    }

关于javascript - 为 Angular 设置 Jasmine/karma,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28184483/

相关文章:

testing - 在控制台中使用 Jasmine 2 和 Protractor 3.0.0 进行规范名称和测试

coffeescript - 无法运行最简单的规范 : TypeError: '[object Object]' is not a constructor (evaluating 'new Options()' )

javascript - 我如何使用 JS 在 HTML/CSS 中完成 1° 和 359° 之间的旋转过渡

javascript - 单击时使用 Angular JS 或 JS 显示隐藏链接

angularjs - Karma - 单元测试 Angular JS 指令时 div 的 ui 属性不正确

angularjs - Angular Material : md-datepicker CSS display issue

javascript - 结合 Angular 和 React 来简化

javascript - 在 Node.js 中包含单独的路由文件

javascript - 在附加 att id 后用 jquery 打开链接

javascript - 如何在 WordPress 插件中调用 AJAX?