Skip to content

Instantly share code, notes, and snippets.

@0xjmp
Last active August 29, 2015 14:14
Show Gist options
  • Save 0xjmp/cdfcdb208ae99722e162 to your computer and use it in GitHub Desktop.
Save 0xjmp/cdfcdb208ae99722e162 to your computer and use it in GitHub Desktop.
An example of a unit test (jasmine) of a service and controller
// 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});
};
};
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 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