如何在Capybara步骤中包含Rails视图助手

ber*_*kes 4 view-helpers capybara

我有一个步骤定义:

Then(/^I should see the total price of the products$/) do
  # assumes all existing products are in the cart
  total = Product.find(:all).inject { |sum, p| sum + p.price }
  page.find(".total").should have_content number_to_currency(total)
end
Run Code Online (Sandbox Code Playgroud)

这爆发了 undefined method 'number_to_currency' for #<Cucumber::Rails::World:0xbef8d18> (NoMethodError)

我可以通过模拟number_to_currency帮助程序的行为来模拟"total" ,但我宁愿重用Rails中的帮助程序,因为在这一步中我对格式化不感兴趣,只是计算和渲染总数.

如何在Capybara中包含NumberHelper或任何视图助手,以便从步骤定义进行访问?

小智 5

我把帮助器包含在我的黄瓜测试中的方式:

  • 您在支持文件夹下创建文件world.rb(或任何您想要的名称).在我的world.rb中我定义如下:

    module Helper
    
       include ProjectHelper
       include ProductBacklogHelper
       include ApplicationHelper
       include Capybara::DSL
       include Prickle::Capybara 
    
    end #end Module  
    World(Helper)
    
    Run Code Online (Sandbox Code Playgroud)

您应该在world.rb中包含具有"number_to_currency"函数的帮助程序文件

我希望这能帮到您