Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created June 18, 2014 03:38
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 miguelmota/84bb25d09dcb20e3ca53 to your computer and use it in GitHub Desktop.
Save miguelmota/84bb25d09dcb20e3ca53 to your computer and use it in GitHub Desktop.
Angular.js $httpBackend example
(function(module) {
module.controller('TestController', ['$scope', '$http',
function($scope, $http) {
$http.get('/api/stuffs')
.then(function(data) {
$scope.stuffs = data;
});
}]);
}(angular.module('testApp')));
describe('testApp' function () {
beforeEach(module('testApp'));
describe('testController', function() {
var scope, httpBackend, http, controller;
beforeEach(inject(function($rootScope, $controller, $httpBackend, $http) {
scope = $rootScope.$new();
httpBackend = $httpBackend;
http = $http;
controller = $controller;
httpBackend.when("GET", '/api/stuffs').respond(200, ['a',1,{}];
}));
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
it('should GET stuffs', function() {
httpBackend.expectGET('/api/stuffs');
controller('TestController', {
$scope: scope,
$http: http
});
httpBackend.flush();
assert(scope.stuffs.length, 3)
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment