Skip to content

Instantly share code, notes, and snippets.

@leviwilson
Last active Aug 29, 2015
Embed
What would you like to do?
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