我有一些使用自定义参数的步骤定义。
const assertEntity = function(name: string, operator: string,
otherName: string) {
console.log(`assertAttrs with ${name} ${operator} ${otherName}`);
};
Then("{name} object is {operator} {otherName}", assertEntity);
以及以下功能文件(已 chop )
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"
});
但是 cucumber 说步骤定义未定义...
? 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';
});
我相信问题出在我的正则表达式中,但我在示例中看到了这一点,所以我不确定如何继续。
最佳答案
变形金刚的工作原理
- 正则表达式必须与参数匹配
- cucumber 表达式必须与转换回正则表达式时的步骤匹配
您可以使用任何类型的转换。例如:
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 匹配的正则表达式的最终结果,但它是一个相当标准的正则表达式在 Cucumber 表达式随 3.x.x 一起出现之前,可以在步骤定义中找到,因此我使用的 2 个示例就在其中。
关于javascript - cucumber JS : Custom parameter types not matching,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52594271/