gpt4 book ai didi

angularjs - angular beforeEach 注入(inject)导致简单测试失败

转载 作者:行者123 更新时间:2023-12-04 16:06:52 27 4
gpt4 key购买 nike

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

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 file foo should be equal to 1 FAILED



但是,如果我像这样注释掉注入(inject)行:

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',

最佳答案

它通过的原因是因为你真的没有测试任何东西。在测试可以实际运行之前,您需要对 Controller 进行更多操作。我认为如果你像下面这样构造东西,它应该可以工作:

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);
});
});


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

关于angularjs - angular beforeEach 注入(inject)导致简单测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37870168/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com