From: Gustavo Martin Morcuende Date: Sat, 29 Aug 2015 23:18:58 +0000 (+0200) Subject: showcase: cars.controller.spec creating spy without object X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=195ac9b0e2d13dda0c376a619f57e3b6ad6ebfc9;p=JavaScriptForFun showcase: cars.controller.spec creating spy without object --- diff --git a/angularjs/showcase/src/showcase/app/cars/cars.controller.spec.js b/angularjs/showcase/src/showcase/app/cars/cars.controller.spec.js index 74c8cc8..d04427b 100644 --- a/angularjs/showcase/src/showcase/app/cars/cars.controller.spec.js +++ b/angularjs/showcase/src/showcase/app/cars/cars.controller.spec.js @@ -2,6 +2,7 @@ describe('app.cars', function() { 'use strict'; var Cars; + // Why the heck do I need this stupid object if it is going to be spied by means of Jasmine? var cars = { getAll: function() { return {}; @@ -12,9 +13,10 @@ describe('app.cars', function() { beforeEach(function() { module('app.cars'); - inject(function($controller, $location, _$q_) { + inject(function($controller, $modal, $timeout, _$q_) { Cars = $controller('Cars', { - $location: $location, + $modal: $modal, + $timeout: $timeout, cars: cars }); $q = _$q_; @@ -23,7 +25,7 @@ describe('app.cars', function() { describe('Cars controller', function () { - it('should invoke GET all cars in service', function () { + it('should invoke GET all cars in service: old fashionable way', function () { spyOn(cars, 'getAll') .and.callFake(function() { @@ -38,3 +40,42 @@ describe('app.cars', function() { }); }); + +describe('app.cars', function() { + 'use strict'; + + var Cars; + // With object already implementing the required spy :) + var cars = { + getAll: jasmine.createSpy('cars.getAll').and.callFake(function() { + return $q(function(resolve) { + resolve(); + }); + }) + }; + var $q; + + beforeEach(function() { + module('app.cars'); + + inject(function($controller, $modal, $timeout, _$q_) { + Cars = $controller('Cars', { + $modal: $modal, + $timeout: $timeout, + cars: cars + }); + $q = _$q_; + }); + }); + + describe('Cars controller', function () { + + it('should invoke GET all cars in service: alternative way', function () { + + Cars.getCars(); + + expect(cars.getAll).toHaveBeenCalled(); + }); + }); + +});