是否可以在函数内定义一个Jasmine规范,并且仍然有beforeEach适用于它?

Mar*_*ley 7 javascript unit-testing jasmine

我有很多测试几乎都是一样的.为了DRY和可扫描性,我想将测试抽象为单个函数,然后使用一些参数调用该函数.然后,该函数将调用it并将规范添加到套件中.

它似乎有效,除了规范不会以与其他规范相同的方式运行,并且beforeEach在公共函数中定义的规范之前不会调用.

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'
            });
        });

    });

});
Run Code Online (Sandbox Code Playgroud)

是否有这样做的,维护的功能的任何方式beforeEachafterEach

更新:

看起来我的例子错了,抱歉.这是失败的情况:

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'
            });
        });

    });
});
Run Code Online (Sandbox Code Playgroud)

我认为它失败了,因为值module被保留作为调用的一部分commonTests而不是总是使用module第一个示例中的当前值.当我到达那里时,我会发布我的解决方案......

Mar*_*ley 5

感谢Andreas指出我的第一个示例确实有效!我使用的最终解决方案非常相似:

define(['modules/MyModule'], function(MyModule) {

    var module;

    function commonTests(params) {
        it('will not fail because module is shared', function() {
            expect(typeof module).toBe('object');
            expect(typeof module[params.method]).toBe('function');
        });

        it('has a few tests in here', function() {
            expect(true).toBe(true);
        });
    }


    describe('MyModule', function() {

        beforeEach(function() {
            module = new MyModule();
        });

        describe('#function1', function() {
            commonTests({
                method: 'function1'
            });
        });

        describe('#function2', function() {
            commonTests({
                method: 'function2'
            });
        });

    });
});
Run Code Online (Sandbox Code Playgroud)

虽然如果您需要具有module作为参数传递的能力,commonTests则必须采取稍微不同的方法并对每个it块使用不同的功能:

define(['modules/MyModule'], function(MyModule) {

    var module;

    function commonTest1(params) {
        expect(typeof module).toBe('object');
        expect(typeof module[params.method]).toBe('function');
    }

    function commonTest2(params) {
        expect(true).toBe(true);
    }


    describe('MyModule', function() {

        beforeEach(function() {
            module = new MyModule();
        });

        describe('#function1', function() {

            it('will not fail because module is shared', function() {
                commonTest1({ method: 'function1' });
            });

            it('has a few tests in here', function() {
                commonTest2({ method: 'function1' });
            });

        });

        describe('#function2', function() {

            it('will not fail because module is shared', function() {
                commonTest1({ method: 'function2' });
            });

            it('has a few tests in here', function() {
                commonTest2({ method: 'function2' });
            });

        });

    });
});
Run Code Online (Sandbox Code Playgroud)

这样,包含公共测试的功能的执行将延迟到每个Each运行其回调之前。