在下文中,我在@variable
中存储了example 1
的值。但它在example 2
中是空的。
class GeographicLocationTest < Minitest::Test
describe "geographic location" do
it "example 1" do
@variable = "Sample variable"
end
it "example 2" do
puts "string"
puts @variable
end
end
end
任何人都会对我出错的地方提出建议。
答案 0 :(得分:2)
实例变量不在it
示例之间共享。您可以使用before
块:
class GeographicLocationTest < Minitest::Test
describe "geographic location" do
before(:each) do
@variable = "Sample variable"
end
it "example 1" do
expect(@variable).to eq "Sample variable"
end
it "example 2" do
expect(@variable).to eq "Sample variable"
end
end
end
在before方法中初始化。它将适用于此处it
中的每个describe
。
如果要在每个测试中为实例变量设置不同的值,则需要在那里重新分配。
答案 1 :(得分:1)
使用before
hook:
class GeographicLocationTest < Minitest::Test
describe "geographic location" do
before do
@variable = "Sample variable"
end
it "example 1" do
# no need of the following line now
@variable = "Sample variable"
end
it "example 2" do
puts "string"
puts @variable
end
end
end
有关documentation的更多信息。