javascript - 是否可以在一个函数中定义一个 Jasmine 规范并且仍然有 beforeEach 应用于它?

标签 javascript unit-testing jasmine

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

它似乎可以工作,除了规范不会以与其他规范相同的方式运行,并且 beforeEach 不会在公共(public)函数中定义的规范之前被调用。

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

    });

});

有没有办法做到这一点并保持 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'
            });
        });

    });
});

我认为它失败了,因为 module 的值被保留为调用 commonTests 的一部分,而不是始终使用 module 的当前值> 和第一个例子一样。当我到达那里时,我会发布我的解决方案...

最佳答案

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

    });
});

尽管如果您需要能够将 module 作为参数传递给 commonTests,您将不得不采取稍微不同的方法,并且每个方法都有不同的函数 block :

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

        });

    });
});

这样,包含公共(public)测试的函数的执行会延迟到 beforeEach 运行其回调之后。

关于javascript - 是否可以在一个函数中定义一个 Jasmine 规范并且仍然有 beforeEach 应用于它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13139977/

相关文章:

javascript - 在 "it" block 之外运行代码会破坏我的 Jasmine 测试

javascript - 在 jQuery 中绑定(bind)多个事件

node.js - 让 Jest 在测试时忽略 .less 导入

angular - 在 Angular/Jasmine 单元测试中绕过 *ngIf

c++ - 如何对 void 函数进行单元测试 CPPUNIT

unit-testing - 如何按类别过滤 Visual Studio 2012 中的单元测试?

angularjs - 如何使用 Jasmine 测试 AngularJS 服务?

javascript - Vue 3 在设置中使用函数

php - javascript 数组弹出

javascript - 使用 javascript 显示 CAD 图像