Cucumber JS:自定义参数类型不匹配 [英] Cucumber JS: Custom parameter types not matching

查看:47
本文介绍了Cucumber JS:自定义参数类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some step definitions that use custom parameters.

const assertEntity = function(name: string, operator: string, 
                                                   otherName: string) {
    console.log(`assertAttrs with ${name} ${operator} ${otherName}`);
};

Then("{name} object is {operator} {otherName}", assertEntity);

以及以下特征文件(截断)

And the following feature file (truncated)

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

但是黄瓜说步骤定义未定义...

However cucumber says step definition is undefined...

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

我认为问题出在我的正则表达式中,但我已经在示例中看到了这一点,所以我不确定如何继续.

I believe the problem is in my regexp but I've seen this done in examples so I'm not sure how to proceed.

推荐答案

变形金刚的工作原理

  1. 正则表达式必须匹配参数
  2. 在转换回正则表达式时,黄瓜表达式必须与步骤匹配

您可以使用任何种类的转换.例如:

You can use any variety of transformations. For instance:

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 的数组中.

The transformation that is happening here is the url is being located in an array of various urls.

答案

对于您的示例,您提供的步骤定义与您提供的步骤不匹配.

For your example, the step definition that you have provided wouldn't match the step that you have provided.

但是,如果我们继续匹配这个:

But, if we were to go ahead and match this:

Given we have a new ApiKey called "Red"

通过使用这样的步骤定义:

By using a step definition like this:

Given('we have a new ApiKey called {name}', function(){
     return pending
});

我们需要这样的步进转换器:

We would require a step transformer like this:

defineParameterType({
    regexp: /"([^"]*)"/,
    transformer: function(s) {
        return s;
    },
    name: "name",
    useForSnippets: false
});

注意:"([^"]*)" 不是与 Cucumber 匹配的正则表达式的全部,但它是一个相当标准的正则表达式在黄瓜表达式出现 3.xx 之前的步骤定义中可以找到,因此我使用的 2 个示例与它们一起使用.

Note: "([^"]*)" is not the be-all end-all of regex matching with Cucumber, but it was a fairly standard regex to be found within the step definitions before cucumber expressions came out with 3.x.x, hence the 2 examples I've used are with them.

这篇关于Cucumber JS:自定义参数类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆