【问题标题】:Not a function (mocking error?)不是函数(模拟错误?)
【发布时间】:2015-09-20 10:55:45
【问题描述】:

我正在尝试使用 Karma-jasmine 框架对我的 Ionic 应用程序进行一些单元测试,这让我很头疼。 我的控制器中有一个功能,我想通过给它一些虚拟输入(凭据)来测试它,但我不断收到错误

TypeError: 'undefined' is not a function (evaluating '$controller')
TypeError: 'undefined' is not an object (evaluating '$scope.credentials.username = "uname"')

我尝试了一些替代方法来做到这一点,但其中一种一直给我一些虚假的信息,我需要从我的services.js 添加依赖项(即$ionicPopup),这可能有些道理,但我对这一切都很陌生。任何帮助将不胜感激。

通过大部分重写,我取得了很大的进步,但我现在遇到了错误

TypeError: 'undefined' is not a function (evaluating 'LoginService.initiateRequest(
                          $scope.credentials.username,
                          $scope.credentials.password)')
                   at c:/Users/name/git/mobile/www/js/controllers.js:18

这是说它不是我的 controllers.js 文件中的一个函数……它肯定是。实在看不懂这个。跟服务有关系吗?

这里有我的controllers.tests.js 文件...

describe('Controllers', function() {

    beforeEach(module('myApp.services', 'myApp.controllers'));

    var mockRequest = {

    }
    beforeEach(function() {
        module(function($provide) {
            $provide.value('initiateRequest', mockRequest);
        });
    });
    var $rootScope, $controller, $sharedProperties, $resource;
    beforeEach(inject(function(_$controller_, $rootScope) {
        myScope = $rootScope.$new();
        $scope = {};
        $state = {};
        LoginService = {};
        localStorageService = {};
        sharedProperties = {};
        $ionicLoading = myScope;
        initiateRequest = mockRequest;
        $controller = _$controller_;
    }));
    describe('$scope.login()', function() {
        it('does something', function() {
            var $scope = {};
            var controller = $controller('LoginController', {
                $scope: $scope,
                $state: $state,
                LoginService: LoginService,
                localStorageService: localStorageService,
                sharedProperties: sharedProperties,
                $ionicLoading: $ionicLoading
            });
            expect($scope.login).toBeDefined(true);
            expect($scope.show).toBeDefined(true);
            $scope.login();
        });

    });
});

【问题讨论】:

  • 为什么您的控制器以. 开头?它不应该是 app.controller() 之类的东西
  • 括号和大括号在controllers.tests.js中不平衡
  • @Barmar D'oh!不敢相信我错过了。

标签: javascript unit-testing ionic karma-jasmine


【解决方案1】:
describe('Controllers', function () {

  beforeEach(module('myApp.services', 'myApp.controllers'));

  var mockRequest = {};

  beforeEach(function () {
    module(function ($provide) {
      $provide.value('initiateRequest', mockRequest);
    });
  });
  var $controller, sharedProperties, state, scope, ionicLoading, LoginService, localStorageService, initiateRequest;
  beforeEach(function () {
    inject(function ($rootScope, _$controller_) {
      scope = $rootScope.$new();
      $controller = _$controller_;
      ionicLoading = {};
      sharedProperties = {};
      LoginService = {};
      localStorageService = {};
      initiateRequest = mockRequest;
    });
  });
  describe('$scope.login()', function () {
    beforeEach(function () {
      $controller('LoginController', {
        $scope: scope,
        $state: state,
        LoginService: LoginService,
        localStorageService: localStorageService,
        sharedProperties: sharedProperties,
        $ionicLoading: ionicLoading
      });
      scope.$apply();
    });
    it('does something', function () {
      expect(scope.login).toBeDefined();
      expect(scope.show).toBeDefined();
    });
  });
});

【讨论】:

  • 这主要是可行的,但仍然给我“未定义不是函数(评估......)”的问题。
猜你喜欢
  • 1970-01-01
  • 2022-01-23
  • 2019-05-14
  • 1970-01-01
  • 2021-05-24
  • 2023-03-27
  • 2013-05-28
  • 2018-01-29
  • 1970-01-01
相关资源
最近更新 更多