This is an example meant to provide the beginning of unit testing a user like Angular resource.
Created
December 27, 2014 21:58
-
-
Save trevorackerman/d5697347910b962e16ba to your computer and use it in GitHub Desktop.
Angular Resource Unit Test
This file contains hidden or 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
// envionment specific configurations settings that can be overridden in testing | |
angular.module('myapp.config', []) | |
.constant('ENV', {name:'development',restApiHost:'localhost',restApiPort:3000}); |
This file contains hidden or 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
module.exports = function(config){ | |
config.set({ | |
basePath : './', | |
files : [ | |
'app/bower_components/angular/angular.js', | |
'app/bower_components/angular-route/angular-route.js', | |
'app/bower_components/angular-resource/angular-resource.js', | |
'app/bower_components/angular-mocks/angular-mocks.js', | |
'app/config.js', | |
'app/components/user/user.js' | |
'app/components/user/user_test.js' | |
], | |
autoWatch : true, | |
frameworks: ['jasmine'], | |
browsers : ['Chrome'], | |
plugins : [ | |
'karma-chrome-launcher', | |
'karma-firefox-launcher', | |
'karma-jasmine', | |
'karma-junit-reporter' | |
], | |
junitReporter : { | |
outputFile: 'test_out/unit.xml', | |
suite: 'unit' | |
} | |
}); | |
}; |
This file contains hidden or 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
'use strict'; | |
// A way to provide the location of external services | |
angular.module('myapp.location', ['myapp.config']) | |
.provider("locations", function () { | |
var config; | |
this.setValue = function (val) { | |
config = val; | |
}; | |
this.$get = function () { | |
return config; | |
}; | |
}) | |
.config(function (ENV, locationsProvider) { | |
locationsProvider.setValue(ENV); | |
}); |
This file contains hidden or 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
'user strict'; | |
angular.module('myapp.services', ['ngResource', 'myapp.location']) | |
.factory('user', function($resource, locations) { | |
var restApiSite = 'http://' + locations.restApiHost + ':' + locations.restApiPort | |
return $resource(restApiSite + '/users'); | |
}); |
This file contains hidden or 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
'use strict'; | |
ddescribe('myapp.user module', function() { | |
var userResource, $httpBackend; | |
beforeEach(function() { | |
angular.mock.module("myapp.config", function ($provide) { | |
$provide.constant("ENV", {restApiHost: "somehost", restApiPort: 1234}); | |
}); | |
angular.mock.module("myapp.location", function() {}); | |
angular.mock.module('myapp.services'); | |
}); | |
beforeEach(function () { | |
angular.mock.inject(function ($injector) { | |
$httpBackend = $injector.get('$httpBackend'); | |
userResource = $injector.get('user'); | |
}) | |
}); | |
describe('user resource', function() { | |
it('should call the Saturn REST api when signing up a user', function() { | |
$httpBackend | |
.whenPOST('http://somehost:1234/users', { | |
name: 'John Wayne', | |
email: '[email protected]', | |
password: 'foobarbazpop', | |
password_confirmation: 'foobarbazpop' | |
}) | |
.respond(200); | |
$httpBackend.expectPOST('http://somehost:1234/users', {name:'John Wayne', | |
email:'[email protected]', | |
password:'foobarbazpop', | |
password_confirmation:'foobarbazpop'}); | |
userResource.save({name:'John Wayne', | |
email:'[email protected]', | |
password:'foobarbazpop', | |
password_confirmation:'foobarbazpop'}); | |
$httpBackend.flush(); | |
}) | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment