angular beforeEach inject导致简单测试失败

时间:2016-06-16 22:09:32

标签: angularjs mocking jasmine

每当我尝试注入角度服务时,我的简单Jasmine测试就会失败。但是,如果我删除注入,它的工作原理。以下内容会导致抛出错误:



describe('rule.js file', function() {
  beforeEach(module('myModule'));
  beforeEach(inject(function($controller) {
    console.log('What the heck is wrong???: ' + $controller);
  }));
  it('foo should be equal to 1', function() {
    expect(1).toBe(1);
  });
});



 这是错误:

  

PhantomJS 2.1.1(Windows 7 0.0.0)rule.js文件foo应该等于1 FAILED

但是,如果我像这样注释掉注入线:



describe('rule.js file', function() {
  beforeEach(module('myModule'));
  it('foo should be equal to 1', function() {
    expect(1).toBe(1);
  });
});




测试通过。

任何人都有任何可能出错的线索?

编辑:这是我的karma.conf包含的文件

  'bower_components/angular/angular.js',
  'bower_components/angular-animate/angular-animate.js',
  'bower_components/angular-aria/angular-aria.js',
  'bower_components/angular-cookies/angular-cookies.js',
  'bower_components/angular-messages/angular-messages.js',
  'bower_components/angular-resource/angular-resource.js',
  'bower_components/angular-route/angular-route.js',
  'bower_components/angular-sanitize/angular-sanitize.js',
  'bower_components/angular-touch/angular-touch.js',
  'bower_components/angular-ui-router/release/angular-ui-router.js',
  'bower_components/angular-strap/dist/angular-strap.js',
  'bower_components/angular-strap/dist/angular-strap.tpl.js',
  'bower_components/angular-deferred-bootstrap/angular-deferred-bootstrap.js',
  'bower_components/angular-ui-router.stateHelper/statehelper.js',
  'bower_components/lz-string/libs/lz-string.min.js',
  'bower_components/angular-ui-scroll/dist/ui-scroll.js',
  'bower_components/angular-ui-scrollpoint/dist/scrollpoint.js',
  'bower_components/angular-ui-event/dist/event.js',
  'bower_components/angular-ui-mask/dist/mask.js',
  'bower_components/angular-ui-validate/dist/validate.js',
  'bower_components/angular-ui-indeterminate/dist/indeterminate.js',
  'bower_components/angular-ui-uploader/dist/uploader.js',
  'bower_components/angular-ui-utils/index.js',
  'bower_components/angular-recaptcha/release/angular-recaptcha.js',
  'bower_components/angular-local-storage/dist/angular-local-storage.js',
  'bower_components/angular-bowser/src/angular-bowser.js',
  'bower_components/ngAnimate-animate.css/animate.js',
  'bower_components/angular-truncate/src/truncate.js',
  'bower_components/blob-polyfill/Blob.js',
  'bower_components/file-saver.js/FileSaver.js',
  'bower_components/angular-file-saver/dist/angular-file-saver.bundle.js',
  'bower_components/ng-idle/angular-idle.js',
  'bower_components/jasmine/lib/jasmine-core/jasmine.js',
  'bower_components/angular-mocks/angular-mocks.js',
  "app/scripts/viewlevels.js",
  "app/scripts/global.js",
  "app/scripts/hrbui.js",
  "app/scripts/hrb-mask.js",
  'app/scripts/**/*.js',
  'unit-tests/global.js',
  'unit-tests/specs/**/*.js',

1 个答案:

答案 0 :(得分:0)

它之所以通过是因为你真的没有测试任何东西。在测试实际运行之前,您需要对控制器进行更多操作。我认为如果你构建如下的东西,它应该有效:

describe('rule.js file', function() {
  var controller;
  beforeEach(module('myModule'));
  beforeEach(inject(function(_$controller_) {
    controller = _$controller_('yourControllerName', {$scope: $scope});
    
    console.log('What the heck is wrong???: ' + controller);
  }));
  it('foo should be equal to 1', function() {
    expect(1).toBe(1);
  });
});

我似乎对答案有点迟了,但希望这可以帮助别人。