在if语句中测试函数 - AngularJS / Jasmine

时间:2016-06-27 13:25:16

标签: angularjs unit-testing jasmine code-coverage

我测试了大部分代码,但我很难弄清楚如何测试这个函数complete

if ($rootScope.associates == null) {
      Papa.parse('file', {
        download: true,
        header: true,
        dynamicTyping: true,
        complete: function(results, file) { // CODE NOT TESTED STARTS HERE
          $rootScope.associates = results;
        }
      });
    }

如果需要,我可以发布一些测试,但是我不想给你们扔太多代码。有关如何访问该功能以进行测试的任何建议将非常感激:)

编辑:为了清晰起见,添加更多细节。

App.js

angular.module('App', [
    'ngAnimate',
    'ngCookies',
    'ngMessages',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch',
    'ngFileUpload',
    'ui.bootstrap',
    'mp.autoFocus'
  ])
  .config(function($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/login.html',
        controller: 'LoginCtrl'
      })
      .when('/login', {
        templateUrl: 'views/login.html',
        controller: 'LoginCtrl'
      })
      .when('/new_user', {
        templateUrl: 'views/new_user.html',
        controller: 'NewUserCtrl'
      })
      .when('/about', {
        templateUrl: 'views/about.html',
        controller: 'AboutCtrl'
      })
      .when('/package_tracking', {
        templateUrl: 'views/package_tracking.html',
        controller: 'PackageTrackingCtrl'
      })
      .when('/batch_receive', {
        templateUrl: 'views/batch_receive.html',
        controller: 'BatchReceiveCtrl'
      })
      .when('/query', {
        templateUrl: 'views/query.html',
        controller: 'QueryCtrl'
      })
      .when('/home', {
        templateUrl: 'views/home.html'
      })
      .otherwise({
        redirectTo: '/'
      });
  })
  .run(function($rootScope, $location, $http, $cookies) {

    $rootScope.associates = null;

    //Preload all the associates information
    if ($rootScope.associates == null) {
      Papa.parse('file', {
        download: true,
        header: true,
        dynamicTyping: true,
        complete: function(results, file) {
          $rootScope.associates = results;
        }
      });
    }

测试用例

    describe('Testing routeCheck', function() {
  beforeEach(module('buildingServicesApp'));

  var location, route, rootScope, httpBackend;

  beforeEach(inject(
    function(_$location_, _$route_, _$rootScope_) {
      location = _$location_;
      route = _$route_;
      rootScope = _$rootScope_;
    }));

  describe('Login route', function() {
    beforeEach(inject(
      function($httpBackend) {
        httpBackend = $httpBackend;
      }));

    it('should reroute to the login page on other page request when not logged in',
      function() {
        httpBackend.expectGET('conf.json').respond(200);
        httpBackend.expectGET('views/login.html').respond(200);
        location.path('/query');
        rootScope.$digest();
        expect(route.current.templateUrl).toBe('views/login.html');
      });
    it('should load the new user page on new user page request',
      function() {
        httpBackend.expectGET('conf.json').respond(200);
        httpBackend.expectGET('views/new_user.html').respond(200);
        location.path('/new_user');
        rootScope.$digest();
        expect(route.current.templateUrl).toBe('views/new_user.html');
      });
    it('should load the login page on login page request', function() {
      httpBackend.expectGET('conf.json').respond(200);
      httpBackend.expectGET('views/login.html').respond(200);
      location.path('/login');
      rootScope.$digest();
      expect(route.current.templateUrl).toBe('views/login.html');
    });
    it('should load the query page when logged in', function() {
      httpBackend.expectGET('conf.json').respond(200);
      httpBackend.expectGET('views/query.html').respond(200);
      rootScope.loggedIn = true;
      location.path('/query');
      rootScope.$digest();
      expect(route.current.templateUrl).toBe('views/query.html');
    });
  });
});

0 个答案:

没有答案