Skip to content

Instantly share code, notes, and snippets.

@jhiemer
Last active December 13, 2015 23:49
Show Gist options
  • Save jhiemer/4994477 to your computer and use it in GitHub Desktop.
Save jhiemer/4994477 to your computer and use it in GitHub Desktop.
Testing Controller and Services with require.js and Angular
/**
* 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;
});
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);
});
});
});
})
/**
* 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('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();
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment