【问题标题】:Struggling to get started on AngularJS unit testing努力开始 AngularJS 单元测试
【发布时间】:2016-05-16 17:29:38
【问题描述】:

我按照这篇文章 (http://gonehybrid.com/how-to-write-automated-tests-for-your-ionic-app-part-2/) 使用 Karma 和 Jasmine 为 Ionic 控制器创建了一个简单的单元测试,但是在定义了所述对象时,我不断收到未定义的错误。我错过了一些明显的东西?顺便说一句,我能够成功地从上面的博客中运行引用的测试,这让我觉得我错过了一些东西。

错误如下:

  1. TypeError: undefined is not an object (evaluating 'authMock.login') in /Users/projects/app/tests/unit-tests/login.controller.tests.js(第 65 行)
  2. TypeError: undefined is not an object (evaluate 'deferredLogin.resolve') in /Users/projects/app/tests/unit-tests/login.controller.tests.js(第 71 行)
  3. TypeError: undefined is not an object (evalating 'deferredLogin.reject') in /Users/projects/app/tests/unit-tests/login.controller.tests.js(第 79 行)

这是控制器:

    angular.module('app').controller('LoginCtrl', function($scope, $state, $ionicPopup, $auth) {
  $scope.loginData = {};
  $scope.user = {
      email: '',
      password: ''
    };

    $scope.doLogin = function(data) {
      $auth.login(data).then(function(authenticated) {
        $state.go('app.tabs.customer', {}, {reload: true});
      }, function(err) {
        var alertPopup = $ionicPopup.alert({
          title: 'Login failed!',
          template: 'Please check your credentials!'
        });
      });
    };

});

这是测试:

    describe('LoginCtrl', function() {

    var controller,
        deferredLogin,
        $scope,
        authMock,
        stateMock,
        ionicPopupMock;

    // load the module for our app
    beforeEach(angular.mock.module('app'));

    // disable template caching
    beforeEach(angular.mock.module(function($provide, $urlRouterProvider) {
        $provide.value('$ionicTemplateCache', function(){} );
        $urlRouterProvider.deferIntercept();
    }));

    // instantiate the controller and mocks for every test
    beforeEach(angular.mock.inject(function($controller, $q, $rootScope) {
        deferredLogin = $q.defer();

    $scope = $rootScope.$new();

        // mock dinnerService
        authMock = {
            login: jasmine.createSpy('login spy')
                          .and.returnValue(deferredLogin.promise)
        };

        // mock $state
        stateMock = jasmine.createSpyObj('$state spy', ['go']);

        // mock $ionicPopup
        ionicPopupMock = jasmine.createSpyObj('$ionicPopup spy', ['alert']);

        // instantiate LoginController
        controller = $controller('LoginCtrl', {
            '$scope': $scope,
            '$state': stateMock,
            '$ionicPopup': ionicPopupMock,
                        '$auth': authMock
          });

    }));

    describe('#doLogin', function() {

        // call doLogin on the controller for every test
        beforeEach(inject(function(_$rootScope_) {

            $rootScope = _$rootScope_;

            var user = {
                email: 'test@yahoo.com',
                password: 'test'
            };

            $scope.doLogin(user);

        }));

        it('should call login on $auth Service', function() {

            expect(authMock.login).toHaveBeenCalledWith(user);
        });

        describe('when the login is executed,', function() {
            it('if successful, should change state to app.tabs.customer', function() {

                deferredLogin.resolve();
                $rootScope.$digest();

                expect(stateMock.go).toHaveBeenCalledWith('app.tabs.customer');
            });

            it('if unsuccessful, should show a popup', function() {

                deferredLogin.reject();
                $rootScope.$digest();

                expect(ionicPopupMock.alert).toHaveBeenCalled();
            });
        });
    })
});

这是我的 Karma 配置:

    files: [
  '../www/lib/ionic/js/ionic.bundle.js',
  '../www/lib/angular-mocks/angular-mocks.js',
  '../www/js/*.js',
  '../www/js/**/*.js',
  'unit-tests/**/*.js'
],

【问题讨论】:

  • 很惊讶没有答案。有没有测试大师?

标签: angularjs unit-testing ionic-framework karma-jasmine


【解决方案1】:

我认为您的测试控制器未定义。尝试首先用它替换它的功能并检查它是否已定义。

it('controller to be defained', function() {
            expect($controller).toBeDefined();
});

如果不是,请尝试调用控制器:

$controller = _$controller_;

【讨论】:

    猜你喜欢
    • 2016-06-19
    • 2014-05-20
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多