[FIX] web: QUnit context in js tests

Previously, the context for `options.{before,after}Each()` functions was
isolated from the context created by the odoo `test` function, making
it impossible to save attributes in the test during
`options.beforeEach()` that are accessible from the test itself, or to
clear resources created in the test in `options.afterEach()`

Moreover, the odoo test infrastructure was set up completely inside the
test wrapper, after `options.beforeContext()` had already run, making
it impossible for `options.beforeContext()` to use this infrastructure.

Here we use the QUnit context directly in the test, but move the
initialization previously done in the test wrapper to a `beforeEach`
wrapper (and, symetrically, on `afterEach`), making it possible to use
the odoo infrastructure in the javascript test setup, including
declared section dependencies.
This commit is contained in:
Leonardo Rochael Almeida
2017-03-03 10:17:33 +01:00
committed by Géry Debongnie
parent 2580a6d04d
commit bf8cc11e7e
+28 -13
View File
@@ -81,8 +81,33 @@ odoo.define_section = function (name, section_deps) {
section_body = arguments[3];
}
QUnit.module(name, options);
mock = new odoo.testing.MockRPC();
function dummyfunc () {};
function beforeEach(assert) {
var services = odoo.testing.start_services();
this.deps = services;
this.getServiceDeps = function getServiceDeps(dep_names) {
return _.map(dep_names, function (name) { return services[name]; })
}
services['web.core'].qweb.add_template(odoo.testing.templates);
this.mock = mock;
this.mock.interceptRPC(services['web.session']);
this.assert = assert;
(options.beforeEach || dummyfunc).apply(this, [assert].concat(this.getServiceDeps(section_deps)));
}
function afterEach(assert) {
(options.afterEach || dummyfunc).apply(this, [assert].concat(this.getServiceDeps(section_deps)));
this.mock.clear();
}
QUnit.module(name, {
before: options.before,
beforeEach: beforeEach,
afterEach: afterEach,
after: options.after
});
function test () {
var name = arguments[0],
@@ -90,21 +115,11 @@ odoo.define_section = function (name, section_deps) {
body = arguments[arguments.length - 1];
QUnit.test(name, function (assert) {
var services = odoo.testing.start_services();
var deps = _.map(section_deps.concat(dep_names), function (name) { return services[name]; });
services['web.core'].qweb.add_template(odoo.testing.templates);
mock.clear();
mock.interceptRPC(services['web.session']);
var info = {
assert: assert,
mock: mock,
deps: _.pick.apply(null, [services].concat(section_deps).concat(dep_names)),
}
return body.apply(info, [assert].concat(deps));
var deps = this.getServiceDeps(section_deps.concat(dep_names));
return body.apply(this, [assert].concat(deps));
});
}
var mock = new odoo.testing.MockRPC();
section_body(test, mock);
};