Browse Source

angjs ex with mochachai

master
Ganesh 5 years ago
parent
commit
6892491f66
9 changed files with 2821 additions and 0 deletions
  1. 2
      angjs-mocha-1/app.js
  2. 41
      angjs-mocha-1/index.html
  3. 60
      angjs-mocha-1/karma.conf.js
  4. 2566
      angjs-mocha-1/package-lock.json
  5. 47
      angjs-mocha-1/package.json
  6. 18
      angjs-mocha-1/simpleController.js
  7. 33
      angjs-mocha-1/simpleController.spec.js
  8. 19
      angjs-mocha-1/simpleService.js
  9. 35
      angjs-mocha-1/simpleService.spec.js

2
angjs-mocha-1/app.js

@ -0,0 +1,2 @@
angular
.module('app', []);

41
angjs-mocha-1/index.html

@ -0,0 +1,41 @@
<!DOCTYPE html>
<html>
<head>
<!-- stylesheets -->
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
<link href="//cdnjs.cloudflare.com/ajax/libs/mocha/1.13.0/mocha.css" rel="stylesheet" />
<!-- library scripts -->
<script src="//cdnjs.cloudflare.com/ajax/libs/mocha/1.13.0/mocha.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/chai/1.10.0/chai.min.js"></script>
<script src="//sinonjs.org/releases/sinon-1.12.2.js"></script>
<script src="//code.angularjs.org/1.4.0-beta.4/angular.js"></script>
<script>
mocha.setup({
"ui": "bdd",
"reporter": "html"
});
</script>
<script src="//code.angularjs.org/1.4.0-beta.4/angular-mocks.js"></script>
<script src="//code.jquery.com/jquery-2.1.3.min.js"></script>
<!-- project scripts -->
<script src="app.js"></script>
<script src="simpleService.js"></script>
<script src="simpleController.js"></script>
<!-- tests -->
<script src="simpleService.spec.js"></script>
<script src="simpleController.spec.js"></script>
</head>
<body>
<div class="container" style="margin-top:30px;">
<h1>AngularJS Unit Test Example using Mocha, Chai and Sinon</h1>
<div id="mocha"></div>
</div>
<script>
mocha.run();
</script>
</body>
</html>

60
angjs-mocha-1/karma.conf.js

@ -0,0 +1,60 @@
// Karma shared configuration
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// list of files / patterns to load in the browser
files: [
'node_modules/angular/angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'./*.js'
],
// list of files / patterns to exclude
exclude: [],
/* Enable / disable watching file and executing tests
* whenever any file changes
*/
autoWatch: true,
// testing framework to use (jasmine/mocha/qunit/...)
// as well as any additional frameworks (requirejs/chai/sinon/...)
frameworks: [
'mocha',
'chai',
'sinon',
'chai-sinon'
],
logLevel: config.LOG_INFO,
// Shared plugins (default is 'karma-*'):
plugins: [
'karma-*'
],
// web server port
port: 8080,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: [
'Chrome'
],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true
});
};

2566
angjs-mocha-1/package-lock.json
File diff suppressed because it is too large
View File

47
angjs-mocha-1/package.json

@ -0,0 +1,47 @@
{
"name": "plunkr_example",
"version": "1.0.0",
"description": "Basic testing example converted from jasmine to mocha",
"main": "mocha",
"scripts": {
"test": "karma start"
},
"repository": {
"type": "git",
"url": "https://github.com/moelders/javascript-testing.git"
},
"devDependencies": {
"angular": "^1.8.1",
"angular-mocks": "^1.8.1",
"sinon": "^9.2.0",
"karma": "^4.4.1",
"mocha": "^8.2.0",
"chai": "^4.2.0",
"sinon-chai": "^3.5.0",
"karma-chai": "0.1.0",
"karma-chai-sinon": "0.1.5",
"karma-chrome-launcher": "3.1.0",
"karma-mocha": "2.0.1",
"karma-sinon": "1.0.5",
"karma-coverage": "^2.0.3"
},
"engines": {
"node": ">=0.10.0"
},
"license": {
"type": "Apache-2.0",
"description": "Apache License Version 2.0, January 2004",
"url": "http://www.apache.org/licenses/LICENSE-2.0"
},
"keywords": [
"test",
"testing",
"unit",
"coverage",
"karma",
"mocha",
"chai",
"sinon",
"chrome"
]
}

18
angjs-mocha-1/simpleController.js

@ -0,0 +1,18 @@
(function () {
'use strict';
angular
.module('app')
.controller('SimpleController', Controller);
function Controller(SimpleService) {
var vm = this;
initController();
function initController() {
// do something with the simple service
SimpleService.DoSomething();
}
}
})();

33
angjs-mocha-1/simpleController.spec.js

@ -0,0 +1,33 @@
describe('SimpleController', function () {
// define variables for the services we want to access in tests
var SimpleController,
SimpleService;
beforeEach(function () {
// load the module we want to test
module('app');
// get services from injector
inject(function ($controller, _SimpleService_) {
SimpleService = _SimpleService_;
// spy on service method to check if it gets called later
sinon.spy(SimpleService, 'DoSomething');
// get controller instance from $controller service
SimpleController = $controller('SimpleController');
});
});
afterEach(function(){
// remove spy from service
SimpleService.DoSomething.restore();
});
describe('constructor', function () {
it('should do something with the SimpleService', function () {
// Assert
assert(SimpleService.DoSomething.calledOnce);
});
});
});

19
angjs-mocha-1/simpleService.js

@ -0,0 +1,19 @@
(function () {
'use strict';
angular
.module('app')
.factory('SimpleService', Service);
function Service($log) {
var service = {
DoSomething: doSomething
};
return service;
function doSomething() {
$log.info('something done!');
}
}
})();

35
angjs-mocha-1/simpleService.spec.js

@ -0,0 +1,35 @@
window.assert = chai.assert;
describe('SimpleService', function () {
// define variables for the services we want to access in tests
var SimpleService,
$log;
beforeEach(function () {
// load the module we want to test
module('app');
// inject the services we want to test
inject(function (_SimpleService_, _$log_) {
SimpleService = _SimpleService_;
$log = _$log_;
})
});
describe('#DoSomething', function () {
it('should log the message "something done!"', function () {
// Arrange
sinon.spy($log, 'info');
// Act
SimpleService.DoSomething();
// Assert
assert($log.info.calledOnce);
assert($log.info.calledWith('something done!'));
// Cleanup
$log.info.restore();
});
});
});
Loading…
Cancel
Save