I've been grappling with unit testing in Angular and have hit a few obstacles along the way. While trying to set up a plnkr for my question regarding a failing Jasmine test that's making an $httpbackend call, I encountered a different error. It would be incredibly helpful if someone could assist me in resolving this plunkr conundrum, as it may also lead to a solution for the original issue. You can find the question here: Jasmine test fails making $httpbackend call and the plunkr here: http://plnkr.co/edit/lJTx0ldR9nEnlYbU5pJd
(function(){'use strict';
var RestService = function($http, $rootScope){
var _postData = function(url, params, data, successFunction, errorMsg, errorFunction, config) {
if(config && config.hasOwnProperty('showLoader')){
$rootScope.showLoader = config.showLoader;
}
$http({
method: 'POST',
url: url,
params: params,
data: data,
cache: false
})
.success(function(data, status, headers, config) {
$rootScope.showLoader = false;
if (successFunction === undefined) {
_defaultSuccessFunction(data, status, headers, config);
}
else {
successFunction(data, status, headers, config);
}
})
.error(function (data, status, headers, config) {
$rootScope.showLoader = false;
if(status === 401){
_processError(data, status, headers, config, errorMsg, errorFunction);
}
});
};
return {
postData: _postData
};
};
angular.module('ram-utilities.ui.rest.service', []).factory('RestService', ['$http', '$rootScope', RestService]);
})();
(function(){ 'use strict';
var LoginController = function($scope, RestService){
var _user = undefined;
var _message = 'hello';
var _login = function(user){
var _success = function(response){
_message = response.success;
_user = response.user;
};
var _error = function(response){
_message = response.success;
};
RestService.postData('/api/login', null, {username: user.username, password: user.password}, _success, 'Invalid login, please try again', _error, {showLoader: true});
};
$scope.model = {
login: _login,
user: _user,
message: _message
};
};
angular.module('danny',['ram-utilities.ui.rest.service']).controller('LoginController',['$scope', 'RestService',LoginController]);
})();
describe('LoginController', function(){
var scope, $httpBackend, controller, restService;
beforeEach(function(){
module('danny');
});
beforeEach(inject(function(_$controller_, _$rootScope_, _$httpBackend_, _RestService_){
$httpBackend = _$httpBackend_;
restService = _RestService_;
scope = _$rootScope_.$new();
controller = _$controller_('LoginController', {
$scope: scope,
RestService: restService
});
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
describe('successfully logging in', function(){
it('should redirect to /blog when authenticated', function(){
var user = {"username":"example@example.com", "password":"test"};
expect(user.username).toEqual('example@example.com');
$httpBackend.expectPOST('/api/login', user).response(200, {});
scope.model.login(user);
$httpBackend.flush();
expect(scope.model.user).not.toBe(undefined);
});
});
});
(function() {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 250;
/**
Create the `HTMLReporter`, which Jasmine calls to provide results of each spec and each suite. The Reporter is responsible for presenting results to the user.
*/
var htmlReporter = new jasmine.HtmlReporter();
jasmineEnv.addReporter(htmlReporter);
/**
Delegate filtering of specs to the reporter. Allows for clicking on single suites or specs in the results to only run a subset of the suite.
*/
jasmineEnv.specFilter = function(spec) {
return htmlReporter.specFilter(spec);
};
/**
Run all of the tests when the page finishes loading - and make sure to run any previous `onload` handler
### Test Results
Scroll down to see the results of all of these specs.
*/
var currentWindowOnload = window.onload;
window.onload = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
//document.querySelector('.version').innerHTML = jasmineEnv.versionString();
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
}
})();
Thank you for your assistance!