在Cucumber Ruby中与Capybara跨步骤共享实例变量



我是Cucumber和Ruby的新手,我想检查数据在页面上是否可见:

Given I am a user
And I own a thing
When I visit the page
Then I can see my thing
<<p>步骤/strong>
Given ("I am a user") do 
@user = FactoryBot.create(:user)
end
And ("I own a thing") do 
@thing = FactoryBot.create(:thing, user: @user)
end
When ("I visit the page") do 
visit page_path
end
Then ("I can see my thing") do 
expect(page).to have_text(@thing.name)
end

结果

undefined method `name' for nil:NilClass

expect(page).to have_text(@thing.name)
^^^^^^ (NoMethodError)

我尝试了以下操作,但没有成功:

When ("I visit the page") do 
visit (page_path($thing))
end

我知道实例变量在页面上不可用,但我不知道如何注入它。

对象是通过关联创建的,需要通过它的父对象访问,所以在我的例子中是@user。事情就是解决办法。

最新更新