【问题标题】:Share instance variable accross steps in Cucumber Ruby with Capybara在 Cucumber Ruby 和 Capybara 中跨步骤共享实例变量
【发布时间】:2022-06-11 13:36:44
【问题描述】:

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

功能

Given I am a user
And I own a thing
When I visit the page
Then I can see my thing

步骤

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

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

【问题讨论】:

  • 实例变量应该在其他步骤中可供您使用,它们在 Cucumber World 中设置 - 您确定 create 没有失败吗?此外,我通常不会用And 定义该步骤,因为这看起来只能用And 调用(这些词在技术上都是可以互换的)——而是将其定义为Given ("I own this thing") do ...
  • 还要检查拼写错误,即使在上面的示例中,您也有一个“我拥有一件事”与“我拥有这件事” - 如果您发布真实代码而不是示例,通常会更好可以准确地看到你在做什么对/错

标签: ruby cucumber capybara


猜你喜欢
  • 2019-01-15
  • 2018-02-13
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-11
  • 1970-01-01
相关资源
最近更新 更多