Angular Unit test fails when i try to inject a service

大城市里の小女人 提交于 2019-12-08 02:01:45

问题


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.


回答1:


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)



来源:https://stackoverflow.com/questions/35990679/angular-unit-test-fails-when-i-try-to-inject-a-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!