sps
sps

Reputation: 372

How do I use a predefined variable/constant in my Cucumber testing Scenario

I have defined a variable as userid in env.rb.

userid='1234'

In my Cucumber testing, Scenario, I wish to confirm that my response contains the correct userid. However, I do not wish to hard code the same in my Senario or step definition. Is it possible to do so?

Upvotes: 2

Views: 4098

Answers (2)

moritz
moritz

Reputation: 25767

I would place an additional file, let's say test_constanst.rb in the features/ dir. There, I would define a module like this:

module TestConstants
  def self.user_id
    1234
  end
end

Like this, you have it separated from test configuration and code. You would just have to requrire the file from env.rb.

Upvotes: 3

user1030906
user1030906

Reputation: 21

Variable scope in Ruby is controlled by sigils to some degree. Variables starting with $ are global, variables with @ are instance variables, @@ means class variables, and names starting with a capital letter are constants. Make the variable global and it will be available everywhere, i.e

$userid='1234'

Upvotes: 2

Related Questions