Skip to content

Instantly share code, notes, and snippets.

@jbasinger
Last active October 20, 2015 01:23
Show Gist options
  • Select an option

  • Save jbasinger/877d3608d4dd37fa1b0b to your computer and use it in GitHub Desktop.

Select an option

Save jbasinger/877d3608d4dd37fa1b0b to your computer and use it in GitHub Desktop.
Karma config
files: [
'../lib/ionic/js/ionic.bundle.js',
'../lib/angular-mocks/angular-mocks.js',
'../js/**.js',
'./**.spec.js'
],
function Pizza(slices){
this.slices = slices || 8;
}
angular.module('starter.services', [])
.factory('Pizza', function($http){
return {
orderPizza: function(pizza){
return $http.post('/order',pizza);
}
};
})
describe('Pizza Service', function(){
var pizzaService;
var $httpBackend;
var orderHandler;
beforeEach(function(){
module('starter.services');
inject(function($injector){
pizzaService = $injector.get('Pizza');
$httpBackend = $injector.get('$httpBackend');
orderHandler = $httpBackend.whenPOST('/order').respond(200,true);
});
});
it('should let me order pizza', function(){
expect(pizzaService.orderPizza).toBeDefined();
});
it('should return when I order pizza', function(){
$httpBackend.expectPOST('/order');
var pizzaPromise = pizzaService.orderPizza(new Pizza());
var orderComplete = false;
pizzaPromise.then(function(data){
orderComplete = true;
});
$httpBackend.flush();
expect(orderComplete == true);
});
it('should handle errors', function(){
$httpBackend.expectPOST('/order');
orderHandler.respond(500);
var pizzaPromise = pizzaService.orderPizza(new Pizza());
var gotError =false;
pizzaPromise.catch(function(){
gotError = true;
});
$httpBackend.flush();
expect(gotError == true);
});
});
describe('Pizza', function(){
it("should default to 8 slices", function(){
var p = new Pizza();
expect(p.slices == 8);
});
it("should let me override the number of slices", function(){
var p = new Pizza(12);
expect(p.slices == 12);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment