【发布时间】:2018-05-28 21:08:43
【问题描述】:
我发现自己无法解释的非常奇怪的行为。看起来如果您同时使用 Cucumber 和常量,Ruby 将在场景之间保存局部变量。 在 Cucumber test.feature 文件中,我有这样的步骤
Feature: Test
Scenario Outline: Test outline
Given Set data
|user_id |hash |
|<user_id>|<hash>|
Examples:
|user_id|hash|
|king |xfgh|
Scenario Outline: What is going on
Given Set data
|shop_id|
|<shop_id>|
Examples:
|shop_id|
|554 |
比我有这样的steps.rb文件:
Given(/^Set data$/) do |table|
# table is a table.hashes.keys # => [:smth]
temp = Constants::Cons.dup
table.hashes[0].each do |key, values|
temp[:bodyData][eval(":#{key}")] = values
end
puts("temp: #{temp}")
end
文件 cmodule.rb:
module Constants
Cons =
{
:toService => "Microcontrol",
:bodyData=>{}
}
end
还有文件 env.rb:
require_relative 'cmodule'
World(Constants)
所以,比我运行文件 test.feature 我的输出看起来像这样:
temp: {:toService=>"Microcontrol", :bodyData=>{:user_id=>"king", :hash=>"xfgh"}}
temp: {:toService=>"Microcontrol", :bodyData=>{:user_id=>"king", :hash=>"xfgh", :shop_id=>"554"}}
所以,问题是,为什么我的第二个场景大纲会给出这样的输出:
temp: {:toService=>"Microcontrol", :bodyData=>{:user_id=>"king", :hash=>"xfgh", :shop_id=>"554"}}
而不是这样(这就是我最初从场景中所期望的):
temp: {:toService=>"Microcontrol", :bodyData=>{:shop_id=>"554"}}
但还有更多,我已经开始了一个实验并将我的 cmodule.rb 更改为:
module Constants
def self.cons
{
:toService => "Microcontrol",
:bodyData=>{}
}
end
end
也改了steps.rb:
Given(/^Set data$/) do |table|
# table is a table.hashes.keys # => [:smth]
temp = Constants.cons.dup
table.hashes[0].each do |key, values|
temp[:bodyData][eval(":#{key}")] = values
end
puts("temp: #{temp}")
end
运行 test.feature 后,我得到了:
temp: {:toService=>"Microcontrol", :bodyData=>{:user_id=>"king", :hash=>"xfgh"}}
temp: {:toService=>"Microcontrol", :bodyData=>{:shop_id=>"554"}}
所以,我想不通为什么它适用于 def 而不适用于常量?
【问题讨论】: