我在我的控制器PlanetsController中使用名爲「generate_coordinate」(位於app/helpers/planets_helper.rb中)的方法。如何爲rspec包含/設置可見的幫助器方法?
運行測試時,似乎rspec無法訪問它,所以導致測試套件失敗,因爲行星沒有任何座標。
我想,包括我在utilities.rb文件的開頭幫手,但它沒有工作
include ApplicationHelper
include PlanetsHelper
我也試圖寫utilities.rb文件在我的方法,沒有更多的成功。
我看到這篇文章「Where/how to include helper methods for capybara integration tests」,但它沒有幫助我。
我也讀到「存根」的功能,但因爲我不能明白它可用於,它並沒有幫助我很多......
任何想法?
這裏是我的測試代碼(規格/請求/ 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
當點擊 「驗證」,它導致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
末
感謝您的回答Zubin。我還不清楚代碼的哪一部分可以被代碼的其他部分看到。例如:MycontrollerHelper中的每個方法應該在MycontrollerController中自動可見?該方法是否也可以從「Mycontroller的視圖」中看到?因此,在Helper文件中包含方法而不是直接在Controller文件中插入什麼? – Arnlen 2012-08-12 16:55:32
請參閱已編輯的答案 - 已添加更多信息。 – Zubin 2012-08-13 22:24:07
感謝您的編輯Zubin,非常感謝您花費更多時間解釋我這個機器的時間。 我會嘗試你的兩個解決方案。 :) – Arnlen 2012-08-14 16:40:38