简体   繁体   English

在Karma + Jasmine下使用angular.mock.inject遇到TypeScript AngularJS控制器测试问题

[英]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. 我正在尝试使用Jasmine + Karma测试用TypeScript编写的AngularJS 1控制器。 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) PhantomJS 2.1.1(Windows 8 0.0.0)登录控制器应具有文本“ 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):执行1之1(1失败)错误(0.019秒/0.015秒)

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. 当我将其注释掉并添加一些非常基本的测试时,在angular.mocks.inject方法上似乎失败了。

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). 我已经检查过,并且我肯定在我的文件列表中添加了angular-mocks.js (通常在依赖列表的末尾)。 If it makes any difference im using wiredep to calculate my dependencies. 如果有什么不同,即时通讯使用wiredep计算我的依赖关系。

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. 我只是遇到了同样的错误,最后我设法通过导入.ts文件来修复它,该文件在test.ts文件的顶部创建了模块。 You're getting this error because the .ts file that defines the "app.pages.auth.login" module isn't imported. 您收到此错误消息是因为未导入定义“ app.pages.auth.login”模块的.ts文件。

(And yes that error message is no help at all when solving this.) (是的,解决此错误消息完全没有帮助。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular Jasmine,angular.mock.inject和inject有什么区别? - Angular Jasmine, What is the difference between angular.mock.inject and inject? angular.mock.inject方法的问题 - Issue with angular.mock.inject method angular.mock.module 和 angular.mock.inject 函数在 jasmine 的 beforeAll 中不起作用 - angular.mock.module and angular.mock.inject functions are not working in jasmine's beforeAll 在业力中使用angular.mock.inject给出“ TypeError:无法读取null的属性'$ injector'” - Using angular.mock.inject in karma gives “TypeError: Cannot read property '$injector' of null” angularjs单位测试业力与茉莉花模拟困境 - angularjs unit test karma with jasmine mock dilemma 在业力/茉莉花测试中模拟角度服务/承诺 - mock angular service/promise in a karma/jasmine test 控制器了解服务模拟,但不了解单元测试(AngularJS,Karma-Jasmine) - Controller Knows About Service Mock, but Unit Test Doesn't (AngularJS, Karma-Jasmine) TypeScript Angular Jasmine + Karma测试 - TypeScript Angular Jasmine+Karma test 如何在Karma上使用Jasmine将一个模拟依赖项注入到angular指令中 - How do I inject a mock dependency into an angular directive with Jasmine on Karma 控制器的角度业力茉莉花单元测试 - angular karma jasmine unit test for a controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM