Skip to content

Instantly share code, notes, and snippets.

@peterheard01
Last active March 9, 2016 17:57
Show Gist options
  • Save peterheard01/140b9b979d8a05842724 to your computer and use it in GitHub Desktop.
Save peterheard01/140b9b979d8a05842724 to your computer and use it in GitHub Desktop.
this is a spy test double in javascript
//original Code
function HttpAuthenticator($http){
this.load = (){
return $http.get('/api/login')
};
}
var Authenticator = new HttpAuthenticator();
//the controller which will attempt to log in when it is loaded
angular.module('app').controller(function(Authenticator){
Authenticator.load().then(function(dto){
if(dto.authenticationSuccessful){
$scope.showLoggedInPanel = true;//we want to test this value is set
}
})
});
//Stub Test Double - returns something
function AuthenticatorSuccessStub(){
this.load = function(){
return $.when({authenticationSuccessful:true;});
};
}
//Spy Test Double - sets something
function AuthenticatorSpy(){
var loadedCalled = false;
this.load = function(){
loadedCalled = true;
return $.when({});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment