javascript - cucumber JS : Custom parameter types not matching

标签 javascript typescript cucumber bdd cucumberjs

我有一些使用自定义参数的步骤定义。

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';
     });

我相信问题出在我的正则表达式中,但我在示例中看到了这一点,所以我不确定如何继续。

最佳答案

变形金刚的工作原理

  1. 正则表达式必须与参数匹配
  2. 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/

相关文章:

ruby - 如何在 iPad 模拟器上运行 Cucumber、Ruby 脚本

unit-testing - 如何比较两种环境之间的 Karate 测试结果?

javascript - D3 - 如何在文本选择中获取前一个元素的宽度

javascript - 数组错误 "object is not a function"

javascript - 一个接一个地执行函数

typescript - 在 Ionic 2 中找不到可注入(inject)名称

Angular 2 模块没有导出成员

ruby - 断言 Cucumber 中抛出了一个特定的异常

javascript - 无法在本地主机上的 Action Script 3.0 中使用 Flash ExternalInterface

typescript - 是否可以要求两个不同的 TypeScript 接口(interface)具有相同的键但能够具有不同的值?