From 195ac9b0e2d13dda0c376a619f57e3b6ad6ebfc9 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 30 Aug 2015 01:18:58 +0200 Subject: [PATCH] showcase: cars.controller.spec creating spy without object --- .../src/showcase/app/cars/cars.controller.spec.js | 47 ++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) 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(); + }); + }); + +}); -- 2.1.4