【发布时间】:2012-08-08 01:52:15
【问题描述】:
我在控制器 PlanetsController 中使用名为“generate_coordinate”(位于 app/helpers/planets_helper.rb 中)的方法。
运行测试时,rspec 似乎无法访问它,因此导致我的测试套件失败,因为地球没有任何坐标。
我尝试在 utilities.rb 文件的开头包含我的帮助程序,但没有成功
include ApplicationHelper
include PlanetsHelper
我也尝试将我的方法写入实用程序.rb 文件中,但没有更多成功。
我读了这篇文章“Where/how to include helper methods for capybara integration tests”,但对我没有帮助。
我也读过“存根”函数,但因为我不明白它可以用来做什么,所以对我没有多大帮助......
有什么想法吗?
这是我的测试代码 (spec/requests/planet_pages_spec.rb)
describe "Create planet" do
before do
visit new_planet_path
fill_in "Name", with: "MyPlanet"
click_button "Validate"
end
it {should have_selector('h1', text: "Planet")}
end
当点击“Validate”时,会跳转到PlanetsController,它会调用“generate_coordinate”方法
def create
@planet = Planet.new(name: params[:planet][:name],
coordinates: generate_coordinates, [...])
if @planet.save
redirect_to action: 'index'
else
render 'new'
end
这里是 generate_coordinate 方法,它似乎从未被 rspec 调用过(而当我使用浏览器浏览时)
module PlanetsHelper
def generate_coordinates
coordinates = "0.0.0.0"
end
结束
【问题讨论】:
标签: ruby-on-rails methods rspec visibility helper