Let's take:
When /^(?:|I )fill in the following:$/ do |fields|
fields.rows_hash.each do |name, value|
When %{I fill in "#{name}" with "#{value}"}
end
end
With my rudimentary Ruby knowledge, I was thinking that When
is a method call that takes a regular expression and a block.
But then, I am also thinking that this is a definition, and not a method call, but then how is it achieved? How can When
define something?
The code is the following (code taken from here):
def register_rb_step_definition(regexp, symbol = nil, options = {}, &proc)
proc_or_sym = symbol || proc
RbDsl.register_rb_step_definition(regexp, proc_or_sym, options)
end
When
, Given
, Then
are an alias to register_rb_step_definition
.
You pass a regular expression as an argument, and a block.
Each step definition is registered with the regular expression and the block. When the test is executed, cucumber looks in the previously registered steps and if any regular expression matches it executes the block associated to that regular expression.