I would like to mock a functionality in Karma who returns a file after clicking a download button. I have the following 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";
}
}
The window.location object just call a RESTful service who provides him the desired file directly. And this is, basically, my 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();
});
});
So, my idea is to check when scope.download function is called, if it returns the right url, namely, when I try scope.download(1), the expected answer would be /secure/regulations/1/attachment, roughly.
How should I mock it? Any help is appreciated!
We can run Jasmine tests in a browser ourselves by setting up and loading a HTML file, but more commonly we use a command-line tool called Karma. Karma handles the process of creating HTML files, opening browsers and running tests and returning the results of those tests to the command line.
Jasmine can be classified as a tool in the "Javascript Testing Framework" category, while Karma is grouped under "Browser Testing". "Can also be used for tdd " is the primary reason why developers consider Jasmine over the competitors, whereas "Test Runner" was stated as the key factor in picking Karma.
Jasmine is a behavior-driven development framework for testing JavaScript code that plays very well with Karma. Similar to Karma, it's also the recommended testing framework within the Angular documentation as it's setup for you with the Angular CLI. Jasmine is also dependency free and doesn't require a DOM.
Use $window instead:
var secure = angular.module('secure', []);
secure.controller('ProcedureController', ProcedureController);
ProcedureController.$inject = ['$controller', '$rootScope', '$scope', '$http', '$window'];
function ProcedureController($controller, $rootScope, $scope, $http, $window) {
... // 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";
}
}
describe('ProcedureController', function () {
var windowObj = {location: {href: ''}};
beforeEach(mock.module(function($provide) {
$provide.value('$window', windowObj);
}));
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).toBeDefined();
scope.download(1);
expect(windowObj.location.href).toEqual('/secure/regulations/1/attachment');
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With