Wie man eine window.location-Funktion in Karma / jasmine verspottet
Ich möchte eine Funktion in Karma verspotten, die eine Datei zurückgibt, nachdem Sie auf eine Download-Schaltfläche geklickt haben. Ich habe den folgenden AngularJS-Controller:
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";
}
}
Das window.location-Objekt ruft einfach einen RESTful-Service auf, der ihm die gewünschte Datei direkt zur Verfügung stellt. Und das ist im Grunde mein Try-Test:
describe('ProcedureController', function () {
beforeEach(module('secure'));
beforeEach(inject(function ($rootScope, $http, $controller, $injector) {
scope = $rootScope.$new();
ProcedureController = $controller('ProcedureController', {
$scope: scope,
$http: $http
});
}));
it ('should download something', function() {
expect(scope.download(1)).toBeDefined();
});
});
Also, meine Idee ist es zu überprüfen, wann scope.download -Funktion wird aufgerufen, wenn sie die richtige URL zurückgibt, nämlich wenn ich @ versuc scope.download (1), die erwartete Antwort wäre / secure / rules / 1 / attachment, grob
Wie soll ich es verspotten? Jede Hilfe wird gebeten!