Skip to content

Instantly share code, notes, and snippets.

@cesarandreu
Last active February 6, 2018 17:59
Show Gist options
  • Save cesarandreu/8477132 to your computer and use it in GitHub Desktop.
Save cesarandreu/8477132 to your computer and use it in GitHub Desktop.
Example of how you can test your AngularJS providers.

What is this?

An example of how you can test your AngularJS providers.

Relevant files

  • sampleProvider.js - A very simple configurable provider.
  • sampleProvider_spec.js - Some tests for the provider that should give you an idea of how you might test your real provider

How to run

npm install
bower install
karma start karma.conf.js

Links

{
"name": "sampleProvider Test Pattern",
"version": "1.0.0",
"dependencies": {
"angular": "~1.2.0"
},
"devDependencies": {
"angular-mocks": "~1.2.0",
"angular-scenario": "~1.2.0"
}
}
module.exports = function(config) {
'use strict';
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
frameworks: ['jasmine'],
plugins: [
'karma-jasmine',
'karma-phantomjs-launcher'
],
// list of files / patterns to load in the browser
files: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'sampleProvider.js',
'sampleProvider_spec.js'
],
// list of files to exclude
exclude: [],
// test results reporter to use
// possible values: 'dots', 'progress', 'junit'
reporters: ['progress'],
// web server port
port: 9876,
// cli runner port
runnerPort: 9100,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['PhantomJS'],
// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true
});
};
{
"name": "sampleProvider",
"description": "Shows an example of how you can test your AngularJS providers.",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {
"karma": "~0.10",
"karma-jasmine": "~0.1",
"karma-phantomjs-launcher": "~0.1.1"
},
"engines": {
"node": ">=0.10.0"
}
}
angular
.module('sample', [])
.provider('sample', function() {
'use strict';
var value = 'Default Value';
this.setValue = function(val) {
value = val;
};
this.$get = function() {
var getValue = function() {
return value;
};
var throwValue = function() {
throw new Error(value);
};
return {
getValue: getValue,
throwValue: throwValue
};
};
});
describe('sampleProvider', function() {
'use strict';
// Provider instance
var sample;
// Instanciates the module
beforeEach(function() {
module('sample');
});
// Here we don't do any configuration to our provider
describe('Default Configuration', function() {
beforeEach(function() {
inject(function(_sample_) {
sample = _sample_;
});
});
it('Should get the default value', function() {
expect(sample.getValue()).toBe('Default Value');
});
it('Should throw the default value', function() {
expect(function() {
sample.throwValue();
}).toThrow('Default Value');
});
});
// Here we do some configuration
describe('Configuration A', function() {
// Configure the provider and instanciate it
beforeEach(function() {
module(function(sampleProvider) {
sampleProvider.setValue('A');
});
inject(function(_sample_) {
sample = _sample_;
});
});
it('Should get the configured value', function() {
expect(sample.getValue()).toBe('A');
});
it('Should throw the configured value', function() {
expect(function() {
sample.throwValue();
}).toThrow('A');
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment