我在用摩卡写柏树测试。我有两个应用程序在测试之下,它们是独立的应用程序,但是有一些类似的功能正在测试中。
问题是,我最终得到了两个具有基本相似逻辑的规范文件,唯一的区别是所提供的测试数据,可能还有一些特定于特定应用程序的功能实现。
为了使它可视化,这里是我的规范文件。作为一个例子,我们有狗应用程序和猫应用程序,你可以为你的可爱的狗或猫购买产品。这测试凭单功能,以便您可以得到一些折扣的产品。
我是redeem-voucher-catsapp-spec.ts
import checkoutActions from '../../../support/actions/catsapp/checkout-actions';
import voucherActions from '../../../support/actions/common/voucher-actions';
import {sampleAddressData} from '../../../support/types/checkout-data';
import {localsCatsApp} from '../../../support/types/locals';
import {CheckoutGuestModel, Voucher} from '../../../support/types/types';
context('CatsApp', function() {
const env = Cypress.env('dataEnv');
const loc = localsCatsApp.de;
beforeEach(function() {
cy.fixture(env + '/users').as('users');
cy.fixture(env + '/products').as('products');
cy.fixture(env + '/vouchers').as('vouchers');
});
describe('Redeem voucher as a guest user', function() {
beforeEach(function() {
this.catsVouchers = this.vouchers.catsapp.voucher;
this.url = Cypress.env('catsAppBaseurl') + loc.language;
});
it('I can redeem valid voucher', function() {
const voucher: Voucher = {
code: this.catsVouchers.CHF15.code,
value: this.catsVouchers.CHF15.value,
type: this.catsVouchers.CHF15.type,
threshold: this.catsVouchers.CHF15.threshold
};
const checkoutGuestModel: CheckoutGuestModel = {
url: this.url,
userAddressData: sampleAddressData,
product: this.products.catsapp.standardProduct,
threshold: voucher.threshold
};
checkoutActions.landOnCheckoutPageAsGuest(checkoutGuestModel);
voucherActions.saveCheckoutPricesBeforeApplyingVoucher();
voucherActions.redeemVoucher(voucher, true, loc);
voucherActions.verifyVoucherApplied(voucher);
checkoutActions.finalizeOrder(loc.checkout.orderNowText);
checkoutActions.verifyCheckoutThankYouPage(loc.checkout.thankYouMessage);
});
});
});我是redeem-voucher-dogsapp-spec.ts
import checkoutActions from '../../../support/actions/DogsApp/checkout-actions';
import voucherActions from '../../../support/actions/common/voucher-actions';
import {sampleAddressData} from '../../../support/types/checkout-data';
import {localsDogsApp} from '../../../support/types/locals';
import {CheckoutGuestModel, Voucher} from '../../../support/types/types';
context('DogsApp', function() {
const env = Cypress.env('dataEnv');
const loc = localsDogsApp.de;
beforeEach(function() {
cy.fixture(env + '/users').as('users');
cy.fixture(env + '/products').as('products');
cy.fixture(env + '/vouchers').as('vouchers');
});
describe('Redeem voucher as a guest user', function() {
beforeEach(function() {
this.dogsVouchers = this.vouchers.dogsapp.voucher;
this.url = Cypress.env('DogsAppBaseurl') + loc.language;
});
it('I can redeem valid voucher', function() {
const voucher: Voucher = {
code: this.dogsVouchers.CHF30.code,
value: this.dogsVouchers.CHF30.value,
type: this.dogsVouchers.CHF30.type,
threshold: this.dogsVouchers.CHF30.threshold
};
const checkoutGuestModel: CheckoutGuestModel = {
url: this.url,
userAddressData: sampleAddressData,
product: this.products.dogsapp.standardProduct3,
threshold: voucher.threshold
};
checkoutActions.landOnCheckoutPageAsGuest(checkoutGuestModel);
voucherActions.saveCheckoutPricesBeforeApplyingVoucher();
voucherActions.redeemVoucher(voucher, true, loc);
voucherActions.verifyVoucherApplied(voucher);
checkoutActions.finalizeOrder(loc.checkout.orderNowText);
checkoutActions.verifyCheckoutThankYouPage(loc.checkout.thankYouMessage);
});
});
});正如您所看到的,那里正在进行大量的代码复制。
因此,我试图找到一种方法来编写一个mocha模板规范文件,该文件可以被参数化,并且可以传递特定应用程序的测试数据和函数实现(如凭单对象、url字符串、凭证夹具文件、特定于每个应用程序的checkoutActions类等),然后编写2个规范文件,这些文件将使用特定的参数“调用”我的mocha模板文件。
这和摩卡有关吗?你会怎么做?
(请帮助:)
编辑:这是我尝试过的,灵感来自于提供的答案。但是data.ctx在摩卡context(测试.)函数中似乎没有定义:
export interface VoucherTestData {
ctx: string;
checkoutActions: CheckoutActions;
}
export function testVoucher(data: VoucherTestData) {
context(`Testing ${data.ctx}`, function() {
const env = Cypress.env('dataEnv')
const loc = data.ctx === 'dog' ? localsDogsApp.de : localsCatsApp.de
beforeEach(function() {
cy.fixture(env + '/users').as('users');
cy.fixture(env + '/products').then(function(products) {
this.product = this.products[`${data.ctx}app`]
});
cy.fixture(env + '/vouchers').then(function(vouchers) {
this.testVoucher = vouchers[`${data.ctx}app`].voucher
})
this.url = Cypress.env(`${data.ctx}AppBaseurl${loc.language}`)
})
describe('...', function() {
it('...', function() {
const voucher: Voucher = {
code: this.testVoucher.CHF30.code,
...
}
const checkoutGuestModel: CheckoutGuestModel = {
...
product: this.product.standardProduct3,
};发布于 2021-08-27 22:27:26
在javascript中有两种应用泛型上下文的方法
例如,
['dog', 'cat'].forEach(ctx=> {
context(`Testing ${ctx}`, function() {
const env = Cypress.env('dataEnv')
const loc = ctx === 'dog' ? localsDogsApp.de : localsCatsApp.de
beforeEach(function() {
cy.fixture(env + '/users').as('users');
cy.fixture(env + '/products').then(function(products) {
this.product = this.products[`${ctx}app`]
});
cy.fixture(env + '/vouchers').then(function(vouchers) {
this.testVoucher = vouchers[`${ctx}app`].voucher
})
this.url = Cypress.env(`${ctx}AppBaseurl${loc.language}`)
})
describe('...', function() {
it('...', function() {
const voucher: Voucher = {
code: this.testVoucher.CHF30.code,
...
}
const checkoutGuestModel: CheckoutGuestModel = {
...
product: this.product.standardProduct3,
};https://stackoverflow.com/questions/68956860
复制相似问题