【发布时间】:2021-10-28 03:19:00
【问题描述】:
我正在使用柏树和摩卡。
我想用 mocha 做一些模板,这样我就可以用不同的数据集运行相同的测试。但是由于某种原因,模板字符串不能与 Cypress 和 Mocha 结合使用。
这就是我所拥有的:
import { CheckoutActions } from '../support/actions/common/checkout-actions';
import { CheckoutActionsCats } from '../support/actions/cats/checkout-actions';
export interface VoucherTestData {
ctx: string;
checkoutActions: CheckoutActions;
}
let dataSet: VoucherTestData[] = [
{
ctx: ' cats',
checkoutActions: new CheckoutActionsCats(),
},
];
dataSet.forEach(function (data) {
context(`${data.ctx}`, function () {
console.log('Context data.ctx');
console.log(data.ctx); // Prints cats
if (data.ctx === 'cats') {
console.log('Data is cats');
} else {
console.log('Data failed'); // This is printed. Why?
}
const env = Cypress.env('dataEnv');
beforeEach(function () {
console.log('beforeEach data.ctx');
console.log(data.ctx); // Prints cats
cy.fixture(env + '/users').as('users'); // users is a JSON file
cy.fixture(env + '/products').as('products'); // products is a JSON file
cy.fixture(env + '/vouchers').as('vouchers'); // vouchers is a JSON file
});
describe('My suite', function () {
beforeEach(function () {
console.log('Suite');
console.log(`${data.ctx}`); // Prints cats
this.url = Cypress.env(`${data.ctx}Baseurl`); // This is undefined. If I use Cypress.env('cats'); then it works
this.product = this.products[`${data.ctx}`]; // This is undefined. If I put this.products['cats'] than it is ok
this.testVoucher = this.vouchers[`${data.ctx}`].voucher; // Error "Cannot read property 'voucher' of undefined".
// If i put this.vouchers['cats'].voucher than it is ok
});
it('should do something', function () {
cy.log(this.url); // Prints undefined. Why?
cy.log(data.ctx); // Prints cats
cy.log(this.product.standardProduct);
cy.log(this.testVoucher.CHF15.threshold);
});
});
});
});
有人可以解释为什么与柏树结合使用时这不起作用吗?变量 data.ctx 显然存在,但是当使用模板/插值字符串时,事情停止工作。 这让我很困惑。 请帮忙:)
【问题讨论】:
标签: typescript mocha.js cypress