給定一個像這樣的控制器,它可以創建多個實例變量以供視圖使用,您通常會測試每個變量是否設置正確?這看起來像你想要的,但它似乎也有點可能有點棘手。什麼是正確的方法?使用RSpec測試控制器中的實例變量
class StaffsController < ApplicationController
def index
set_index_vars
@all_staff = Staff.find_staff_for_business_all_inclusive(current_business_id)
respond_to do |format|
format.html { render :action => "index", :locals => { :all_staff => @all_staff, :all_services => @all_services, :new_vacation => @new_vacation } }
end
end
def set_index_vars
@days_of_week = days_of_week
@first_day_of_week = DefaultsConfig.first_day_of_week
@all_services = Service.services_for_business(current_business_id)
@new_vacation = StaffVacation.new
@has_hit_staff_limit = current_user_plan.has_hit_staff_limit?
end
end
的代碼也張貼在https://gist.github.com/1018190