简体   繁体   中英

TypeScript AngularJS controller test issue with angular.mock.inject under Karma + Jasmine

I am trying to test an AngularJS 1 controller written in TypeScript with Jasmine + Karma. But I keep hitting an error I dont know how to fix. The error is quite nondescript:

PhantomJS 2.1.1 (Windows 8 0.0.0) Login Controller should have text" FAILED forEach@/bower_components/angular/angular.js:341:24 loadModules@/bower_components/angular/angular.js:4456:12 createInjector@/bower_components/angular/angular.js:4381:22 workFn@/bower_components/angular-mocks/angular-mocks.js:2507:60 /bower_components/angular/angular.js:4496:53 forEach@/bower_components/angular/angular.js:341:24 loadModules@/bower_components/angular/angular.js:4456:12 createInjector@/bower_components/angular/angular.js:4381:22 workFn@/bower_components/angular-mocks/angular-mocks.js:2507:60 /bower_components/angular/angular.js:4496:53 PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.019 secs / 0.015 secs)

Here is my code, notice that it doesn't really do anything. It seems to fail on the angular.mocks.inject method as when I comment this out and add some very basic tests it works.

describe("Login Controller", () => {
    var controller: App.Login.LoginController;

    beforeEach(angular.mock.module("app.pages.auth.login"));

    beforeEach(angular.mock.module($provide => {
        $provide.value("$window", {});
    }));

    // I can get to here ok but this line below fails with the error above
    // Note I have also tried putting $rootScope back in but to no avail
    beforeEach(angular.mock.inject((/*$rootScope, $state, $http, $mdToast, $window, $log*/) => {
        console.log("test");
    }));

I have checked and I definitely include angular-mocks.js in my file list (albet near the end of my dependency list). If it makes any difference im using wiredep to calculate my dependencies.

I just had the same error and in the end I managed to fix it by importing the .ts file that creates the module at the top of my test.ts file. You're getting this error because the .ts file that defines the "app.pages.auth.login" module isn't imported.

(And yes that error message is no help at all when solving this.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM