gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 04:44:32 25 4
gpt4 key购买 nike

我有很多几乎相同的测试。为了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com