当我尝试注入服务时,Angular Unit测试失败 [英] Angular Unit test fails when i try to inject a service

查看:96
本文介绍了当我尝试注入服务时,Angular Unit测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个角度要使用业力和茉莉花来测试我的控制器和服务.我写了一些测试,他们工作了.然后,我对我的有角度的项目进行了一些大的更改. 现在我的测试都失败了,我不知道为什么.我在测试中注释掉了所有内容,只是想检查$controller服务是否已加载.但这甚至行不通.

我的模块定义:

angular.module('cManagement', [
    'angular-multi-select',
    'ngCookies',
    'ui.router',
    'ui.bootstrap',
    'pascalprecht.translate',
    'ngIdle'
])

这是我的考试:

use strict';
describe("cManagement", function() {

 beforeEach(module('cManagement'));

 it('should exist', inject(function($controller){
 }));
});

我的业力配置:

module.exports = function(config){
    config.set({

        basePath : '',
        reporters: ['progress', 'junit'],
        files : [
            'node_modules/angular/angular.js',
            'node_modules/angular-cookies/angular-cookies.js',
            'node_modules/angular-multi-select/dist/angular-multi-select.js',
            'node_modules/angular-ui-router/release/angular-ui-router.js',
            'node_modules/angular-ui-bootstrap/dist/ui-bootstrap.js',
            'node_modules/angular-translate/dist/angular-translate.js',
            'node_modules/angular-mocks/angular-mocks.js',
            'assets/plugins/angular-idle/angular-idle.js',
            'app/app.module.js',
            'app/components/login/*',
            'app/**/*'
        ],

        singleRun : true,

        frameworks: ['jasmine'],

        browsers : ['PhantomJS'],

        plugins : [
                'karma-jasmine',
                'karma-junit-reporter',
                'karma-phantomjs-launcher'
                ],

        junitReporter : {
            outputDir: 'test_out',
            userBrowserName: false
        }

    });
};

这是我得到的错误:

INFO [karma]:Karma v0.12.37服务器在以下位置启动 http://localhost:9876/ INFO [启动器]:启动浏览器PhantomJS INFO [PhantomJS 2.1.1(Linux 0.0.0)]:连接在ID为71013283的套接字7iIMKn57rTEEJqjikf2o上 PhantomJS 2.1.1(Linux 0.0.0)c管理应该存在失败 /home/chris/IdeaProjects/campaignManagement/node_modules/angular/angular.js:4527:53 forEach@/home/chris/IdeaProjects/campaignManagement/node_modules/angular/angular.js:321:24 loadModules@/home/chris/IdeaProjects/campaignManagement/node_modules/angular/angular.js:4487:12 createInjector@/home/chris/IdeaProjects/campaignManagement/node_modules/angular/angular.js:4409:30 workFn@/home/chris/IdeaProjects/campaignManagement/node_modules/angular-mocks/angular-mocks.js:2799:60 PhantomJS 2.1.1(Linux 0.0.0):执行2之2(1失败)(0.006秒/0.007秒)

每个提示都值得赞赏.

解决方案

很可能您遇到了依赖问题-可能是无法加载的模块-但由于PhantomJS有时隐藏错误消息中的相关信息.

解决方案是(暂时)将测试浏览器更改为Chrome或任何其他非无头浏览器,以确切地找出问题所在.

例如:您应该更改Karma配置或Gruntfile.js或您拥有的内容:

线路browsers: ['PhantomJS'],

变成类似以下内容:browsers: ['Chrome'],

,然后您应该能够看到错误并进行更正.之后,您可以将其切换回PhantomJS.

注意:只是根据 @ snowe2010 的评论放置解决方法(该问题没有答案,所以人们可能会忽略解决方案)

I have a angular where i want to test my controllers and services with karma and jasmine. I wrote some tests and they worked. Then i did some big changes to my angular project. Now my tests are all failing and i can't figure out why. I commented everything out in my tests and just wanted to check if the $controller service gets loaded. But not even this works.

My module definition:

angular.module('cManagement', [
    'angular-multi-select',
    'ngCookies',
    'ui.router',
    'ui.bootstrap',
    'pascalprecht.translate',
    'ngIdle'
])

Here is my test:

use strict';
describe("cManagement", function() {

 beforeEach(module('cManagement'));

 it('should exist', inject(function($controller){
 }));
});

My Karma config:

module.exports = function(config){
    config.set({

        basePath : '',
        reporters: ['progress', 'junit'],
        files : [
            'node_modules/angular/angular.js',
            'node_modules/angular-cookies/angular-cookies.js',
            'node_modules/angular-multi-select/dist/angular-multi-select.js',
            'node_modules/angular-ui-router/release/angular-ui-router.js',
            'node_modules/angular-ui-bootstrap/dist/ui-bootstrap.js',
            'node_modules/angular-translate/dist/angular-translate.js',
            'node_modules/angular-mocks/angular-mocks.js',
            'assets/plugins/angular-idle/angular-idle.js',
            'app/app.module.js',
            'app/components/login/*',
            'app/**/*'
        ],

        singleRun : true,

        frameworks: ['jasmine'],

        browsers : ['PhantomJS'],

        plugins : [
                'karma-jasmine',
                'karma-junit-reporter',
                'karma-phantomjs-launcher'
                ],

        junitReporter : {
            outputDir: 'test_out',
            userBrowserName: false
        }

    });
};

Here is the error i get:

INFO [karma]: Karma v0.12.37 server started at http://localhost:9876/ INFO [launcher]: Starting browser PhantomJS INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket 7iIMKn57rTEEJqjikf2o with id 71013283 PhantomJS 2.1.1 (Linux 0.0.0) cManagement should exist FAILED /home/chris/IdeaProjects/campaignManagement/node_modules/angular/angular.js:4527:53 forEach@/home/chris/IdeaProjects/campaignManagement/node_modules/angular/angular.js:321:24 loadModules@/home/chris/IdeaProjects/campaignManagement/node_modules/angular/angular.js:4487:12 createInjector@/home/chris/IdeaProjects/campaignManagement/node_modules/angular/angular.js:4409:30 workFn@/home/chris/IdeaProjects/campaignManagement/node_modules/angular-mocks/angular-mocks.js:2799:60 PhantomJS 2.1.1 (Linux 0.0.0): Executed 2 of 2 (1 FAILED) (0.006 secs / 0.007 secs)

Every hint is appreciated.

解决方案

Most probably you have a dependency issue -maybe a module that cannot be loaded- but you can't tell exactly which one because PhantomJS sometimes hides relevant info from the error message.

The solution is to change (temporarily) the testing browser to Chrome or any other non-headless browser to see exactly what's wrong.

eg: you should change in Karma config or Gruntfile.js or what have you:

the line browsers: ['PhantomJS'],

into something like: browsers: ['Chrome'],

and then you should be able to see the error and correct it. Afterwards, you can switch it back to PhantomJS.

Note: Just placing for posterity the workaround as per @snowe2010's comment (the question didn't have an answer so people may disregard the solution)

这篇关于当我尝试注入服务时,Angular Unit测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆