Skip to content

Instantly share code, notes, and snippets.

@leviwilson
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leviwilson/03bb346583bf5ee621d0 to your computer and use it in GitHub Desktop.
Save leviwilson/03bb346583bf5ee621d0 to your computer and use it in GitHub Desktop.
myapp.controller('myController', ['myService', function (myService) {
$scope.autofillSearch; //Input field in HTML
$scope.findAutofill = function () {
myService.autofill($scope.autofillSearch)
.success(function (data) {
window.console.log('Yay! ' + data);
})
.error(function () {
window.console.log('BOOO!');
});
}
}]);
describe('MyController', function() {
var myServiceBackend,
subject;
beforeEach(function() {
module('myApp');
inject(function(_myServiceBackend_, $rootScope, $controller) {
subject = $rootScope;
myServiceBackend = _myServiceBackend_;
spyOn(window.console, 'log');
$controller('MyController', {$scope: $rootScope});
});
});
it('can setup responses from the other service', function() {
myServiceBackend.setupAutofill({some: 'thing'})
.toReturn('the data!');
subject.findAutofill();
myServiceBackend.flush();
expect(window.console.log).toHaveBeenCalledWith('Yay! the data!');
});
});
Myapp.service('myService', function($http) {
return {
autofill: function(lookup) {
return $http.post('/autofill/fetch', lookup);
}
}
});
Myapp.service('myServiceBackend', function($httpBackend) {
var stubPromise = function(promise) {
toReturn: function(data) {
promise.respond(data);
},
toFail: function(data) {
promise.respond(500, data);
},
flush: $httpBackend.flush
};
return {
setupAutofill: function(request) {
return stubPromise($httpBackend.expectPOST('/autofill/fetch', request));
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment