Test fails, how to find out why?

若如初见. 提交于 2019-12-08 13:24:00

问题


I am trying to learn testing for angular using Jasmine and Karma, but I have runned into a bit of problem trying to create my first test.

First i got this very simple angular app with a controller named "testCtrl".

angular.module("testApp", [])
.controller("testCtrl", function() {
  this.title = "The Title";
});

I then got this very simple index.html file

<body data-ng-app="testApp">
    <div data-ng-controller="testCtrl as test">
    {{test.title}}
    </div>
</body>

Okay so this far everything works fine. The angular app is running.

Then we got my test.

describe("Testing AngularJS Test Suite", function() {
  describe("Testing AngularJS Controller", function() {

    it("should initialize the title in the scope", function() {
      module("testApp");

      var ctrl;

      inject(function($controller) {
        ctrl = $controller("testCtrl", {});
      });

      expect(ctrl.title).toBeDefined();
      expect(ctrl.title).toBe("The Title");
    });

  });

});

And then the karma configuration file:

// Karma configuration
// Generated on Sat Jun 04 2016 19:05:45 GMT+0200 (CEST)

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine'],
    files: [
      '../../bower_components/angular/angular.js',
      '../../bower_components/angular-mocks/angular-mocks.js',
      '../app.js',
      'units/*.js'
    ],
    proxies : {
      '/' : 'http://localhost:8080/'
    },    
    exclude: [
    ],
    preprocessors: {
    },
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['PhantomJS'],
    singleRun: false,
    concurrency: Infinity
  })
}

And this it what I get when I then start karma.

04 06 2016 19:47:27.770:WARN [config]: "/" is proxied, you should probably change urlRoot to avoid conflicts
04 06 2016 19:47:28.165:WARN [karma]: No captured browser, open http://localhost:9876/
04 06 2016 19:47:28.214:INFO [karma]: Karma v0.13.22 server started at http://localhost:9876/
04 06 2016 19:47:28.248:INFO [launcher]: Starting browser PhantomJS
04 06 2016 19:47:28.882:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket /#ulqwsz9WukaVrW-yAAAA with id 47817079
PhantomJS 2.1.1 (Linux 0.0.0) Testing AngularJS Test Suite Testing AngularJS Controller should initialize the title in the scope FAILED
    some-parentfolders/bower_components/angular/angular.js:4631:53
    forEach@some-parentfolders/bower_components/angular/angular.js:322:24
    loadModules@some-parentfolders/bower_components/angular/angular.js:4591:12
    createInjector@some-parentfolders/bower_components/angular/angular.js:4513:30
    workFn@some-parentfolders/bower_components/angular-mocks/angular-mocks.js:3060:60
    inject@some-parentfolders/bower_components/angular-mocks/angular-mocks.js:3040:46
    some-parentfolders/js/tests/units/testingAngularUnitSpec.js:9:13
PhantomJS 2.1.1 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.049 secs / 0.038 secs)

Big note: This is a big angular application that requires a HTTP-server to run properly. Only this first test is simple. I was not sure if karma setup its own HTTP-server to run the files at or if it just used its HTTP-server for other stuff. And I read somewhere on the web the you should host the file on your own HTTP-server and then point the proxy in the karma config against that url to that server (as I have done above). Not sure about this so please correct me if I am wrong.

Since I dont get any specific error messages I am standing here clue less, and I was hoping someone could tell me what could be worng, where to look or how to start troubleshoting this.

Update:

After trying to run it with Chrome instead of PhantomJS I get these errors:

Error: [$injector:modulerr] Failed to instantiate module testApp due to:
    Error: [$injector:modulerr] Failed to instantiate module some-parentfolders/js/tests due to:
    Error: [$injector:nomod] Module 'some-parentfolders/js/tests' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
    http://errors.angularjs.org/1.5.6/$injector/nomod?p0=some-parentfolders/js/tests

But this dont make sense to me either?


回答1:


PhantomJS may be optimal for unit testing in some environments, but it tends to absorb error messages on some conditions, even with proper logLevel.

For better spec debugging, Chrome launcher may be used with Karma instead.



来源:https://stackoverflow.com/questions/37633373/test-fails-how-to-find-out-why

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