【问题标题】:Testing AngularJS controller with Jasmine causes error in RubyMine用 Jasmine 测试 AngularJS 控制器会导致 RubyMine 出错
【发布时间】:2015-03-02 11:10:16
【问题描述】:

我尝试在 RubyMine 中使用 Jasmine 测试我的 AngularJS 控制器。

这是我的测试

'使用严格';

describe('MainCtrl', function(){ var scope;

beforeEach(module('myModule'));

beforeEach(注入(函数($rootScope) { 范围 = $rootScope.$new(); }));

it('应该定义超过 5 个很棒的东西', 注入(函数($控制器){ 期望(scope.awesomeThings).toBeUndefined();

$controller('MainCtrl', {
  $scope: scope
});

expect(angular.isArray(scope.awesomeThings)).toBeTruthy();
expect(scope.awesomeThings.length > 5).toBeTruthy();   })); });

我使用 Karma 来运行这个测试。为了配置 RubyMine 来运行测试,我做了所有这些教程中写的操作 https://www.jetbrains.com/ruby/webhelp/preparing-to-use-karma-test-runner.html https://www.jetbrains.com/ruby/webhelp/running-unit-tests-on-karma.html

这是我的 Karma 配置文件

'使用严格';

module.exports = 功能(配置){

config.set({ 自动监视:假,

frameworks: ['jasmine'],

browsers : ['PhantomJS'],

plugins : [
    'karma-phantomjs-launcher',
    'karma-jasmine'
]   }); };

但是当我尝试运行测试时出现此错误

src/app/main/main.controller.spec.js:3 describe('MainCtrl', function(){ ^ ReferenceError: describe is not defined

at Object.<anonymous> (src/app/main/main.controller.spec.js:3:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:929:3

我该如何解决这个问题?我很乐意为您提供任何帮助。

【问题讨论】:

  • 您是使用 Karma 运行器还是节点运行器运行测试?
  • 我以为我用的是 Karma runner,但实际上它是 node runner。傻我!但是我现在遇到了一个问题,因为 Karma runner 没有找到我的测试并且我得到“空测试套件”

标签: angularjs karma-runner rubymine karma-jasmine


【解决方案1】:

将此添加到您的插件'karma-spec-reporter' 然后您应该能够运行它 - 我相信它失败了,因为它正在寻找规范报告者。

【讨论】:

  • 我已经在我的插件中添加了“reporter”,但不幸的是这个技巧对我不起作用。
【解决方案2】:

除了使用 karma runner。您需要在配置中添加如下内容:

files: [
  'bower_components/angular/angular.js',
  'bower_components/angular-mocks/angular-mocks.js',
  'js/**/*.js',
  '../../specs/*.js'
],

带有适当的文件列表。您需要包含所有规范以及您需要为您的应用加载的任何文件,例如 angular 和其他库。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 2015-12-14
    • 1970-01-01
    相关资源
    最近更新 更多