通过在jasmine测试中使用angular http服务来测试RestApi

时间:2017-01-27 14:29:04

标签: angularjs gruntjs jasmine karma-runner

我试图通过向服务器发送休息请求并想要验证响应来测试主机。 测试环境使用grunt / karma,测试是在jasmine框架中编写,并使用$ http角度服务向真实服务器发送休息,但似乎http请求没有发送到服务器。 请注意,我不想使用模拟进行测试,而是使用真正的服务器。

附上环境的重要文件。

请建议。

将http请求发送到服务器的Angular代码 - 文件unit.js:

 angular.module('unit_system', [])
.controller('unit_info', ['$scope', '$http', function PasswordController($scope, $http) {
    $scope.siteId = 1;
  $scope.getName = function() {
        $scope.unitName = 'SITE' + $scope.siteId;
  };
  $scope.getInfo = function() {
        $scope.unitName = 'SITE';
        $http({
            method: 'GET',
            url: 'https://10.100.102.102/web_project/index.php/v1/system/generalInfo'
        }).then(function successCallback(response) {
            $scope.unitName = 'SITE1';
            // this callback will be called asynchronously
            // when the response is available
    });
  }
}]);

Jasmine测试 - 文件unit.test.js:

describe('system setup >', function() {
  beforeEach(module('unit_system'));

  var $controller;
  var $http;
  beforeEach(inject(function(_$controller_, _$http_){
    // The injector unwraps the underscores (_) from around the parameter names when matching
    $controller = _$controller_;
    $http = _$http_;
  }));

  describe('unit conf >', function() {
    it('get the unit name', function() {
      var $scope = {};
      var controller = $controller('unit_info', { $scope: $scope, $http: $http });
      $scope.siteId = 99;
      $scope.getName();
      expect($scope.unitName).toEqual('SITE99');
    });

    it('get the unit name', function() {
      var $scope = {};
      var controller = $controller('unit_info', { $scope: $scope, $http: $http });
      $scope.getInfo();
      expect($scope.unitName).toEqual('SITE99');
    });

  });
});

Gruntfile业力部分:     '使用严格的';

module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    // Metadata.
    pkg: grunt.file.readJSON('package.json'),
    banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
      '<%= grunt.template.today("yyyy-mm-dd") %>\n' +
      '<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +
      '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +
      ' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */\n',
    // Task configuration.
    jshint: {
      options: {
        jshintrc: '.jshintrc'
      },
      gruntfile: {
        src: 'Gruntfile.js'
      },
      lib: {
        options: {
          jshintrc: 'lib/.jshintrc'
        },
        src: ['lib/**/*.js']
      },
      test: {
        src: ['test/**/*.js']
      },
    },
    karma: {  
      unit: {
       options: {
         frameworks: ['jasmine'],
         singleRun: true,
         browsers: ['PhantomJS'],
         files: [
           'node_modules/angular/angular.js',
           'node_modules/angular-mocks/angular-mocks.js',
            'src/unit.js',
            'spec/unit.test.js'
         ]
        }
      }
    }
  });

  // These plugins provide necessary tasks.

  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-karma');  

  // Default task.
  grunt.registerTask('test', ['jshint', 'karma']);

};

咕噜声跑步结果:

Running "karma:unit" (karma) task
27 01 2017 16:15:29.158:INFO [karma]: Karma v1.4.0 server started at http://0.0.0.0:9876/
27 01 2017 16:15:29.158:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
27 01 2017 16:15:29.174:INFO [launcher]: Starting browser PhantomJS
27 01 2017 16:15:32.097:INFO [PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on socket OzLnwNVCs8mBemlFAAAA with id 79523492
PhantomJS 2.1.1 (Windows 8 0.0.0) system setup > unit conf > get the unit name FAILED
        Expected 'SITE' to equal 'SITE99'.
        spec/unit.test.js:26:38
        loaded@http://localhost:9876/context.js:151:17
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 2 of 2 (1 FAILED) (0.016 secs / 0.02 secs)
Warning: Task "karma:unit" failed. Use --force to continue.

Aborted due to warnings.

0 个答案:

没有答案