TypeError:undefined不是对象(评估'screen.orientation.lock')

时间:2017-11-21 07:44:52

标签: angularjs ionic-framework gulp karma-runner karma-jasmine

我在m-generator中尝试单元测试。我在测试规范文件中注入了$ scope,但是我遇到了这个错误如何解决这个错误。 我注入了一些文件后,同样的不同注入错误。

控制器文件:

'use strict';
(function () {
 angular.module('main').controller('HomeCtrl', HomeCtrl);

 HomeCtrl.$inject = ['$log', 'SignOut', '$state', '$cordovaNetwork', 
'$auth', 'CommonService', '$scope', '$ionicPopup', 'Analytics', 
'BackupServices', '$translate', '$ionicPlatform'];
 function HomeCtrl ($log, SignOut, $state, $cordovaNetwork, $auth, 
CommonService, $scope, $ionicPopup, Analytics, BackupServices, 
$translate, $ionicPlatform) {
$log.log('HomeCtrl got called');
var vm = this;

vm.password = {
  input: '', // by user
  strength: ''
};
vm.grade = function () {
  var size = vm.password.input.length;
  if (size > 8) {
    vm.password.strength = 'strong';
  } else if (size > 3) {
    vm.password.strength = 'medium';
  } else {
    vm.password.strength = 'weak';
  }
};
vm.grade();

function activate () {
  screen.orientation.lock('portrait');
  Analytics.trackPage('Home-Page');
} })();

这是karma test js文件我注入了homectrl但是homectrl有一些插件,而测试代码确实执行了

测试规范文件:

 describe('module: main, controller: HomeCtrl', function () {

 beforeEach(module('main'));
  beforeEach(module('ngHtml2Js'));

 // instantiate controller
 var HomeCtrl;
  beforeEach(inject(function ($controller, $rootScope) {
  var scope = $rootScope.$new();
  HomeCtrl = $controller('HomeCtrl',{ $scope: scope });
})); 

 describe('.grade()', function () {

it('should classify asd as weak', function () {
  HomeCtrl.password.input = 'asd';
  HomeCtrl.grade();
  expect(HomeCtrl.password.strength).toEqual('weak');
});

it('should classify asdf as medium', function () {
  HomeCtrl.password.input = 'asdf';
  HomeCtrl.grade();
  expect(HomeCtrl.password.strength).toEqual('medium');
});

it('should classify asdfasdfasdf as strong', function () {
  HomeCtrl.password.input = 'asdfasdfasdf';
  HomeCtrl.grade();
  expect(HomeCtrl.password.strength).toEqual('strong');
 });
}); });

1 个答案:

答案 0 :(得分:0)

在您的karma配置路径中包含screenorientation js文件的路径

'插件/科尔多瓦-插件屏幕取向/网络/ screenorientation.js'