Last active
August 29, 2015 14:14
-
-
Save 0xjmp/cdfcdb208ae99722e162 to your computer and use it in GitHub Desktop.
An example of a unit test (jasmine) of a service and controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You would still need to define this in a module | |
// for it to work in a real world environment. | |
this.HomeCtlr = function($scope, Project) { | |
$scope.search = function(query) { | |
$scope.projects = Project.search({query: query}); | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('HomeController', function() { | |
var home; | |
beforeEach(function() { | |
home = module('home'); | |
}); | |
var $httpBackend, $scope, homeController; | |
beforeEach(inject(function(_$httpBackend_, $rootScope, _$controller_, _$resource_, Project) { | |
$httpBackend = _$httpBackend_; | |
$scope = $rootScope; | |
var $controller = _$controller_; | |
homeController = function() { | |
return $controller(this.HomeCtlr, { '$scope' : $scope }); | |
}; | |
})); | |
describe('$scope.search', function() { | |
it('should get results', function() { | |
$httpBackend.expectGET('/api/v1/search/Project.json').respond({projects: []}); | |
homeController(); | |
$scope.search('Project'); | |
$httpBackend.flush(); | |
expect($scope.projects).toBeDefined(); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the service that is used in home_ctlr. It would still need | |
// to be defined to work in a real world environment | |
this.Project = function($resource) { | |
return $resource('/api/v1/projects.:format', { format: 'json'}, { | |
search: { | |
method: 'GET', | |
url: '/api/v1/search/:query.:format', | |
params: { | |
query: '@query' | |
} | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment