Last active
August 29, 2015 14:16
-
-
Save leviwilson/03bb346583bf5ee621d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | |
}); | |
} | |
}]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Myapp.service('myService', function($http) { | |
return { | |
autofill: function(lookup) { | |
return $http.post('/autofill/fetch', lookup); | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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