不是函数(模拟错误?)

时间:2015-07-02 19:00:46

标签: javascript unit-testing ionic karma-jasmine

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

<%= bar_chart(@project_times) %>

我已经尝试了一些替代方法来做到这一点,但是其中一个方法一直给我一些虚假的东西,我需要从我的TypeError: 'undefined' is not a function (evaluating '$controller') TypeError: 'undefined' is not an object (evaluating '$scope.credentials.username = "uname"') 添加依赖项(即{{1这可能有一些道理,但我对这一切都是新手。任何帮助将不胜感激。

我通过重写这个来取得了很大的进步,但我现在收到了错误

services.js

它说它不是我的controllers.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

1 个答案:

答案 0 :(得分:0)

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();
    });
  });
});