小编Hug*_*ako的帖子

如何在Karma/jasmine中模拟window.location函数

我想在Karma中模拟一个功能,它在点击下载按钮后返回一个文件.我有以下AngularJS控制器:

var secure = angular.module('secure', []);
secure.controller('ProcedureController', ProcedureController);
ProcedureController.$inject = ['$controller', '$rootScope', '$scope', '$http'];

function ProcedureController($controller, $rootScope, $scope, $http) {

  ... // Controller does more stuff

  var href = window.location.href.split('/');
  var baseUrl = href[0] + '//' + href[2];
  var url = baseUrl + "/secure/regulations";

  $http.get(url)
    .success(function (data) {
        $scope.questions = data[0];
    })

  $scope.download = function (msg) {
    window.location = url + "/" + msg + "/attachment";
  }
}
Run Code Online (Sandbox Code Playgroud)

window.location对象只调用一个RESTful服务,直接为他提供所需的文件.这基本上就是我的尝试测试:

describe('ProcedureController', function () {
  beforeEach(module('secure'));

  beforeEach(inject(function ($rootScope, $http, $controller, $injector) { …
Run Code Online (Sandbox Code Playgroud)

unit-testing angularjs karma-jasmine

14
推荐指数
1
解决办法
2万
查看次数

标签 统计

angularjs ×1

karma-jasmine ×1

unit-testing ×1