AngularJs 单元测试失败,因为 Karma 无法识别控制器中的 $scope.$on 侦听器



事情是这样的。我一直在调查许多线程,但找不到任何与我的错误有关的内容。我正在尝试测试我的angular应用程序,(我想先澄清一下,我对unitTesting有点新手)

根据本教程,我已经设置好了所有内容,基本测试也很好。但是,当我包含NgMock模块,这是我真正的angular应用程序,并编写一个简单的测试来在我的一个控制器上测试一个方法时,我得到了以下stackTrace。

grunt karma:dev
Running "karma:dev" (karma) task
25 03 2016 20:41:24.754:INFO [karma]: Karma v0.13.22 server started at http://localhost:9876/
25 03 2016 20:41:24.763:INFO [launcher]: Starting browser Chrome
25 03 2016 20:41:24.771:INFO [launcher]: Starting browser Firefox
25 03 2016 20:41:28.747:INFO [Chrome 49.0.2623 (Windows 10 0.0.0)]: Connected on socket /#yRHoLsbEaJWHNtSDAAAA with id 47988329
Chrome 49.0.2623 (Windows 10 0.0.0) Unit tests del CRM:  Create new message Should create a text message with the correct structure FAILED
        TypeError: $scope.$on is not a function
            at new <anonymous> (C:/Users/Pablo/Data/software/workspace/intellij/leadaki-core/public/dashboard/v4/js/controllers/view-conversations.js:248:12)
            at e (C:/Users/Pablo/Data/software/workspace/intellij/leadaki-core/public/dashboard/v4/js/angular.min.js:39:156)
            at Object.instantiate (C:/Users/Pablo/Data/software/workspace/intellij/leadaki-core/public/dashboard/v4/js/angular.min.js:39:273)
            at C:/Users/Pablo/Data/software/workspace/intellij/leadaki-core/public/dashboard/v4/js/angular.min.js:80:228
            at C:/Users/Pablo/Data/software/workspace/intellij/leadaki-core/public/dashboard/v4/js/angular-mocks.js:1848:12
            at Context.<anonymous> (C:/Users/Pablo/Data/software/workspace/intellij/leadaki-core/test/frontend/leadaki-app-tests.js:44:30)
Chrome 49.0.2623 (Windows 10 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.087 secs / 0.024 secs)
Firefox 45.0.0 (Windows 10 0.0.0) Unit tests del CRM:  Create new message Should create a text message with the correct structure FAILED
        $scope.$on is not a function
        @C:/Users/Pablo/Data/software/workspace/intellij/leadaki-core/public/dashboard/v4/js/controllers/view-conversations.js:248:5
        e@C:/Users/Pablo/Data/software/workspace/intellij/leadaki-core/public/dashboard/v4/js/angular.min.js:39:154
        h/<.instantiate@C:/Users/Pablo/Data/software/workspace/intellij/leadaki-core/public/dashboard/v4/js/angular.min.js:39:273
        Xe/this.$get</<@C:/Users/Pablo/Data/software/workspace/intellij/leadaki-core/public/dashboard/v4/js/angular.min.js:80:226
        angular.mock.$ControllerDecorator</<@C:/Users/Pablo/Data/software/workspace/intellij/leadaki-core/public/dashboard/v4/js/angular-mocks.js:1848:12
        @C:/Users/Pablo/Data/software/workspace/intellij/leadaki-core/test/frontend/leadaki-app-tests.js:44:30
Chrome 49.0.2623 (Windows 10 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.087 secs / 0.024 secs)
Firefox 45.0.0 (Windows 10 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.053 secs / 0.011 secs)
Warning: Task "karma:dev" failed. Use --force to continue.
Aborted due to warnings.

ngMock生成的"mock"作用域似乎不接受有监听器之类的东西。

有人暗示这里可能是错的吗?

我可以粘贴我所有的配置文件,但我认为这是太多的信息,我不想泛滥的帖子。如果需要任何其他信息,请告诉我,我会添加它。

提前感谢!

describe('Unit tests del CRM: ', function () {
    before(function() {
        if (window.__html__) {
            document.body.innerHTML = window.__html__['test/index.html'];
        }
    });
    beforeEach(angular.mock.module('appCliengo'));
    var $controller;
    var $rootScope;
    beforeEach(angular.mock.inject(function(_$rootScope_, _$controller_){
        $rootScope = _$rootScope_;
        $controller = _$controller_;
    }));
    describe('Create new message', function () {
        it('Should create a text message with the correct structure', function () {
            var $scope = {};
            var controller = $controller('Contacts.viewConversation', { $scope: $scope });
            var messageBody = 'Hola, me gustaria comprar un auto';
            var newTextMessage = $scope.createMessage('text/plain', '123456789', 'user', 'abcdefghijk', messageBody);
            expect(newTextMessage.body).to.equal(messageBody);
        });
    });
});

如果不查看您的确切设置,很难说,但您是否创建了一个范围对象来注入控制器?

很多时候,你可以通过使用$rootScope对象来伪造它:

beforeEach(angular.mock.inject($injector => {
    $controller = $injector.get('$controller');
    rootScope = $injector.get('$rootScope');
}));
... test code ...
let controller = $controller('UserController', {$scope: rootScope});
... do stuff with controller ...

但是,您也可以使用rootScope创建一个新的作用域并将其输入。

相关内容

  • 没有找到相关文章

最新更新