moro-programmer
2/20/2013 - 10:16 AM

Testing Controller and Services with require.js and Angular

Testing Controller and Services with require.js and Angular

define(['angular', 'mocks'], function () {
  'use strict';

	describe('homeService', function () {
		var homeService, resource, host;

		beforeEach(function () {
			module('services', function ($provide) {
				$provide.factory('ngResource', function ($resource) {

				});

				$provide.factory('REST_HOST', function () {
					host = 'http://locahost:8080/web';

					return host;
				});
			});

			inject(['homeService', '$resource', 'REST_HOST'], function (_homeService, _$resource, _host) {
				homeService = _homeService;
				resource = _$resource;
			});
		});

		describe('verifying get method', function () {
			it('should call the get method', function () {
				homeService.get();
			});
		});
	});
});
/**
 * HomeService
 **/
define(['angular'], function (angular) {
  "use strict";

	var service = function ($resource, REST_HOST) {

		return $resource(REST_HOST + '/home/:id/:property', {}, {
			update: {method:'PUT'},
			query: {method:'GET', isArray:false},
			relation: {method: 'GET', isArray: true}
		});

	};
	service.$inject = ['$resource', 'REST_HOST'];

	return service;
});

define(['angular', 'mocks'], function () {
  "use strict";

  describe('HomeController', function () {
		var homeService, serviceThenSpy, baseScope;

		beforeEach(function () {			
			module('services', function ($provide) {
				$provide.factory('homeService', function () {
					console.log('creating homeService...');

					homeService = jasmine.createSpy('homeService');

					serviceThenSpy = jasmine.createSpy("then").andCallFake(function () {
                        return {
                            then:serviceThenSpy
                        };
                    });

		homeService.get = jasmine.createSpy().andCallFake(function (url) {
					return {
						name : 'blub',
						pass : 'blob' + url,
						then: serviceThenSpy
					};
				});

				console.log('home service created', homeService);

				return homeService;
			});

		});

			module('controllers', function ($provide) {
				$provide.factory('location', function () {
					var location = jasmine.createSpy();

					console.log('location spy', location);

					return location;
				});
			});

			inject(['homeService', '$rootScope', '$controller',
				function(_homeService, $rootScope, $controller) {
					homeService = _homeService;
					console.log(_homeService);					
					baseScope = $rootScope.$new();
					$controller('homeController', {
						$scope : baseScope,
						homeService : homeService
					});
				}
			]);			
		});

		describe('Testing variables', function () {
			it('should have a variable $scope.variable which is "empty"', function () {				
				expect(baseScope.variable).toEqual('empty');
			});
		});

		describe('Testing functions', function () {
			it('should have a function called login', function () {
				expect(baseScope.login()).not.toBe(null);
			});
		});
	});
})
/**
 * HomeController
 **/
 
define(function () {
  'use strict';
  
	function HomeController($scope, homeService) {
		$scope.variable = 'empty';

		$scope.login = function() {
			console.log(homeService);
			console.log('login() was called');
		};
	}

	HomeController.$inject = ['$scope', 'homeService'];

	return HomeController;
});