javascript - Cucumber JS:自定义参数类型不匹配
问题描述
我有一些使用自定义参数的步骤定义。
const assertEntity = function(name: string, operator: string,
otherName: string) {
console.log(`assertAttrs with ${name} ${operator} ${otherName}`);
};
Then("{name} object is {operator} {otherName}", assertEntity);
以及以下功能文件(截断)
Scenario: Compare two similar API key objects
Given we have a new ApiKey called Red
和这样定义的参数类型
defineParameterType({
regexp: /name/,
transformer: function(s) {
return s;
},
name: "name"
});
然而黄瓜说步骤定义未定义......
? Given we have a new ApiKey called Red
Undefined. Implement with the following snippet:
Given('we have a new ApiKey called Red', function () {
// Write code here that turns the phrase above into concrete actions
return 'pending';
});
我相信问题出在我的正则表达式中,但我已经在示例中看到了这一点,所以我不确定如何继续。
解决方案
变形金刚的工作原理
- 正则表达式必须匹配参数
- 转换回正则表达式时,黄瓜表达式必须与步骤匹配
您可以使用任何种类的转换。例如:
Given I am on the "Home" page
Given I am on the "My Basket" page
都可以通过变压器匹配:
defineParameterType({
regexp: /"([^"]*)"/,
transformer(string) {
return urls[string.replace(/ /g, "_").toLowerCase()]
},
name: 'page',
useForSnippets: false
});
这里发生的转换是 url 位于各种 url 的数组中。
答案
对于您的示例,您提供的步骤定义与您提供的步骤不匹配。
但是,如果我们继续匹配这个:
Given we have a new ApiKey called "Red"
通过使用这样的步骤定义:
Given('we have a new ApiKey called {name}', function(){
return pending
});
我们需要一个像这样的步进变压器:
defineParameterType({
regexp: /"([^"]*)"/,
transformer: function(s) {
return s;
},
name: "name",
useForSnippets: false
});
注意:"([^"]*)"
不是所有与 Cucumber 匹配的正则表达式,但它是一个相当标准的正则表达式,可以在黄瓜表达式与 3.xx 出现之前的步骤定义中找到,因此我使用的 2 个示例是跟他们。
推荐阅读
- excel - 我可以分配一个包含工作簿名称的字符串值作为参数吗?
- postgresql - 您可以在 PL/pgSQL 中将日期数组设置为常量吗?
- c# - 如何在.net core3 WPF C#中询问文件名路径
- mysql - 如何在自联接中编写随行的当前迭代而变化的条件
- angular - 在 Angular 7 中使用 Google Map API 根据值加载不同的标记
- aurelia - aureliajs,值转换器不更新`&updateTrigger:'blur'`上的值
- verilog - 以下verilog代码的输出将是什么..?
- regex - RegEx 不匹配 PowerShell 中的序列
- mysql - MYSQL 查询以查找类似于连接字符串的字符串
- javascript - 访问和单击没有 ID 或类标签的按钮