Skip to content

Instantly share code, notes, and snippets.

@yeehaa123
Last active August 29, 2015 14:03
Show Gist options
  • Save yeehaa123/570b6882d24e7447e20d to your computer and use it in GitHub Desktop.
Save yeehaa123/570b6882d24e7447e20d to your computer and use it in GitHub Desktop.
problem with $httpBackend
(function(){
'use strict';
angular.module('cth').factory('Post', ['$http', '$q', Post]);
function Post($http, $q){
function getAll(){
return $http.get('/api/posts.json').then(function(response){
return response.data.posts;
});
}
return {
getAll: getAll
};
}
})();
describe 'Post', ->
beforeEach(module('cth'))
describe 'api calls', ->
beforeEach inject ($injector) ->
@service = $injector.get('Post')
@$httpBackend = $injector.get('$httpBackend')
it "makes an ajax call to '/api/posts.json'", ->
@$httpBackend.expectGET('/api/posts.json').respond({msg: "hello"})
@service.getAll().then (data)->
console.log(data)
@$httpBackend.flush()
describe 'Post', ->
beforeEach(module('cth'))
describe 'api calls', ->
beforeEach inject ($injector) ->
@service = $injector.get('Post')
@$httpBackend = $injector.get('$httpBackend')
it "makes an ajax call to '/api/posts.json'", ->
posts = {}
@$httpBackend.whenGET('/api/posts.json').respond({msg: "hello"})
@service.getAll().then (data)->
posts = data
@$httpBackend.flush()
expect(posts.msg).toBe("Hello")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment