I'm trying to work out how to unit test my login controller with Karma/Jasmine/Mocha.
I basically want to test if a 200 comes back from the $auth.login() then the message saved should be equal to "successfully logged in", otherwise if I receive a 401 then the message that comes back should be "error logging in".
This is where I've gotten up to so far:
login-controller.js
function loginCtrl($auth, $scope, $rootScope) {
var vm = this;
vm.login = function() {
var credentials = { email: vm.email, password: vm.password };
$auth.login(credentials).then(function() {
$scope.message = "successfully logged in";
}, function(error) {
$scope.message = "error logging in";
})
};
}
spec.js
describe('Login Controller', function() {
var rootScope, scope, controller, location, $auth;
beforeEach(function() {
bard.appModule('app.login');
bard.inject('$rootScope', '$controller', '$location', '$auth');
});
beforeEach(function() {
rootScope = $rootScope;
scope = $rootScope.$new();
auth = $auth;
controller = $controller('authCtrl', { $scope: scope });
});
it('should return a success message when successfully logged in', function() {
rootScope.$apply()
expect(scope.message).to.equal("successfully logged in");
});
it('should return an error message when not logged in', function() {
rootScope.$apply()
expect(scope.message).to.equal("error logging in");
});
});
Do I need to explicitly pass the status codes, or is there a better way to mock this?
Any help is appreciated. Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire