I have many tests that are almost the same. In the interest of DRY and the possibility of scanning, I would like to abstract tests for one function, and then call this function with several parameters. Then the function will call it and add the specification to the package.
This seems to work, except that the specifications do not start like other specifications, and beforeEach not called before the specifications defined in the general function.
define(['modules/MyModule','jasmine/jasmine'], function(MyModule) { describe('myModule', function() { function commonTests(params) { it('should pass this test OK', function() { expect(true).toBe(true); }); it('should fail because module is undefined', function() { expect(module[params.method]()).toBe('whatever'); }); } var module; beforeEach(function() { module = new MyModule(); }); describe('#function1', function() { commonTests({ method: 'function1' }); }); describe('#function2', function() { commonTests({ method: 'function2' }); }); }); });
Is there a way to do this and maintain the functionality of beforeEach and afterEach ?
UPDATE:
It looks like I got my example wrong, sorry. Here is a case that fails:
define(['modules/MyModule'], function(MyModule) { function commonTests(params) { it('will fail because params.module is undefined', function() { expect(typeof params.module).toBe('object'); expect(typeof params.module[params.method]).toBe('function'); }); it('has a few tests in here', function() { expect(true).toBe(true); }); } describe('MyModule', function() { var module; beforeEach(function() { module = new MyModule(); }); describe('#function1', function() { commonTests({ module: module, method: 'function1' }); }); describe('#function2', function() { commonTests({ module: module, method: 'function2' }); }); }); });
I think it fails because the module value is saved as part of the commonTests call commonTests instead of always using the current module value, as in the first example. I will send my decision when I get ...