Dredd: Ruby-hooks: execute same hook for multiple requests

157 Views Asked by At

The problem I'm facing is:

I have a request and need to check 3 types of responses for this request. Each time I need to slightly modify my request body before sending it.

dredd --names:
info: Users > User Operations > Update User > Example 1
skip: PUT (204) myurl/users/userid-123
info: Users > User Operations > Update User > Example 2
skip: PUT (422) myurl/users/userid-123
info: Users > User Operations > Update User > Example 3
skip: PUT (429) myurl/users/userid-123

My idea was in the before hook do something like "cucumber-style":

before(/^Users > User Operations > Update User > Example (1|2|3)$/) do |myvar|
  Here run loop from 1 to 3 and do necessary changes

But after several trials this doesn't seem to work, looks like ruby-hooks doesn't support variables in names.

Any ideas what would be the proper approach for this case, since having separate before-hook for every single request doesn't seem right here?

1

There are 1 best solutions below

0
On BEST ANSWER

I don't think the Ruby hooks support regular expressions in the transaction names. A simple workaround would be to catch all and distinguish the transactions in the hook itself:

before_each do |transaction|
  if transaction.name.match(/Example (1|2|3)$/)
    ...
  end
end