From bf8cc11e7e8bae4b3c49e5cb76a1556a6abfdea6 Mon Sep 17 00:00:00 2001 From: Leonardo Rochael Almeida Date: Fri, 30 Dec 2016 11:13:34 -0200 Subject: [PATCH] [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. --- addons/web/static/test/testing.js | 41 +++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/addons/web/static/test/testing.js b/addons/web/static/test/testing.js index 2ec911740b2..46368c38259 100644 --- a/addons/web/static/test/testing.js +++ b/addons/web/static/test/testing.js @@ -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); };