简体   繁体   中英

Cucumber Ruby - Cucumber Expressions

Trying out some new things in Cucumber Ruby, and I came across their Cucumber Expressions.

I've tried registering parameters:

require 'cucumber/cucumber_expressions/parameter_type'
require 'cucumber/cucumber_expressions/parameter_type_registry'

Cucumber::CucumberExpressions::ParameterTypeRegistry.new.define_parameter_type(Cucumber::CucumberExpressions::ParameterType.new(
    'optional_not',
    String,
    /n't| not|/,
    lambda {|s| String.new(s)}
))

Cucumber::CucumberExpressions::ParameterTypeRegistry.new.define_parameter_type(Cucumber::CucumberExpressions::ParameterType.new(
    'string_in_double_quotes',
    String,
    /"[^"]*"/,
    lambda {|s| String.new(s)}
))

But when I have a step definition such as:

Given "{string_in_double_quotes} does{optional_not} work" do |thing, invert|
   invert = invert.gsub(' ', '')
   if invert == 'n\'t' or invert == 'not'
      # Something here
   else
      # Something here
   end
end

It does not match Given "the expression" does work or Given "the expression" does not work - which it should, and simply comes back with a step snippet

Does anyone know what I'm doing wrong?

EDIT

Turns out that the basic "int" cucumber expression is not working either, and that should be built in.

Given "I want {int} potatoes" do |number|
 puts "#{number} potatoes"
end

Does not match And I want 7 potatoes , as an example.

Using:

  • cucumber v3.0.0.pre.1
  • cucumber-expressions v3.0.0
  • ruby v2.4.1

Cucumber expressions are available from v3.0.0 (see: https://github.com/cucumber/cucumber-ruby/issues/1002#issuecomment-332734877 ). However, adding a custom parameter type seems to be undocumented at the moment and I had to dig quite a bit to find out how to do it (see the Cucumber::Glue::Dsl class ).

In my case I wanted to add a date parameter type to match the format YYYY-MM-DD , so I created a new file parameter_types.rb in the support directory (I'm using Cucumber to test a Rails application) with the following content:

ParameterType name:             'date',
              type:             Date,
              regexp:           /\d{4}-\d{2}-\d{2}/,
              transformer:      ->(str) { Date.parse(str) },
              use_for_snippets: true

ParameterType is a method like After , Before , etc. that you can use to define hooks. You can add more parameter types by appending them to the same file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM