On Wednesday, December 12, 2012 4:37:52 AM UTC+1, Davis Ford wrote:
Each release of angular also releases angular-mock.js, so sure, you can use angular-mock. But, if you want to use angular.mock.module and angular.mock.inject, support for mocha wasn't introduced until release 1.1.1.
What I meant was: use angular-mocj 1.1.1 with angular 1.0.x... But actually, module and inject aren't supported, even in that version. I managed to get it to work, though, at least in sync mode. Here's my setup:
In app.js, I create a separate app for the tests:
if (MODE == 'development') {
ss.client.define('tests', {
view: 'tests.html',
css: ['mocha.css'],
code: ['libs', 'app', 'tests'],
tmpl: '*'
});
ss.http.route('/tests', function(req, res){
res.serveClient('tests');
});
}
With this config, all files and folders in code/app are available for the tests, and the code/tests/entry.js overrides code/app/entry.js
tests:
+- entry.js
+- libs:
| +- 128.expect.js
| +- 256.mocha.js
| +- 320.mocha.setup.bdd.js
| +- 384.angular-mocks.js
+- specs:
+- test.js
The angular/jQuery libs are loaded in code/libs, before code/tests/libs is loaded.
mocha.setup.bdd.js contains one line: mocha.setup('bdd'); This must be done before loading angular-mocks.
The view is pretty simple. Specifically:
[classic SS header.]
<body>
<div id="mocha"></div>
</body>
For the controller, I inject the app rather than requiring it (a trick I've read on this list before):
code/app/controllers.js:
module.exports = funcfion(ngModule) {
ngModule.controller(...)
}
It allows me to inject as many angular modules as I want during testing.
code/app/entry.js
foo = angular.module('foo');
require('./controller')(foo);
code/tests/specs/test.js
var bar;
beforeEach(function(){
bar = module('bar'+counter())
require('../controllers')(bar)
})
code/tests/entry.js
$(function(){
require('./specs/test')
mocha.run();
})
At last, in order to get mock.module and .inject to work, you must change isSpecRunning in angular-mock.js, line 1624:
function isSpecRunning() {
return currentSpec && currentSpec.queue && currentSpec.queue.running
|| currentSpec; // for Mocha.
}
I don't know how reliable the latter is, but it works for sync tests.