2011-06-10 14 views
7

के साथ नियंत्रक में परीक्षण आवृत्ति चर परीक्षण इस तरह के नियंत्रक को देखते हुए जहां यह दृश्य द्वारा उपयोग के लिए कई उदाहरण चर बनाता है, क्या आप आम तौर पर परीक्षण करेंगे कि उनमें से प्रत्येक ठीक से सेट हो जाए? ऐसा लगता है कि आप चाहते हैं, लेकिन यह थोड़ा सा लगता है कि यह थोड़ा मुश्किल हो सकता है। सही दृष्टिकोण क्या है?आरएसपीसी

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

उत्तर

11

आप हाँ, हर तरह से तो एक नियंत्रक कल्पना लिखने के लिए, जा रहे हैं परीक्षण है कि उदाहरण चर सौंपा है। 'Trickiness' की ज्यादातर अन्य मॉडलों/पुस्तकालयों पर निर्भरता से आते हैं, इसलिए उन विधि कॉल बाहर ठूंठ कर सकते हैं:

# air code 
Staff.stub(:find_staff_for_business_all_inclusive) {array_of_staff} 
controller.stub(:days_of_week) {['Monday','Tuesday',....etc...]} 
DefaultsConfig.stub(:first_day_of_week) {"Monday"} 
Service.stub(:services_for_business).with(some_value_for_the_current_business_id).\ 
    and_return(some_relevant_value) 
StaffVacation.stub(:new) {something_meaningful} 
controller.stub_chain(:current_user_plan,:has_hit_staff_limit?) {false} 

get :index 
assigns(:days_of_week).should == ['Monday','Tuesday',....etc...] 
# ...etc... 
0

पर पोस्ट किया जाता है, तो आप, परीक्षण कर सकते हैं सही मूल्यों आदि कुछ के साथ कि अपने विधि सही समय पर बुलाया जा रहा है जैसे:

describe StaffsController do 
    describe "GET #index" do 
    it "should call set_index_vars" do 
     controller.should_receive(:set_index_vars) 
     get :index 
    end 
    end 

    describe "#set_index_vars" do 
    it "should assign instance variables with correct values" do 
     # or wtv this is supposed to do 
     get :index 
     assigns(:days_of_week).should == controller.days_of_week 
     # etc .. 
    end 
    end 
end 
1

मैं इसे विभाजित देंगी: परीक्षण है कि index कॉल सही विधि। फिर परीक्षण करें कि विधि काम करती है या नहीं।

तो जैसे

describe StaffsController do 
    describe "GET #index" do 
    it "calls set_index_vars" do 
     controller.should_receive(:set_index_vars) 
     get :index 
    end 
    # and your usual tests ... 
    end 

    describe "#set_index_vars" do 
    before(:each) do 
     # stub out the code not from this controller 
     controller.stub_chain(:current_user_plan, :has_hit_staff_limit?).and_return(false) 
     .. etc .. 

     controller.set_index_vars 
    end 
    it { assigns(:days_of_week).should == controller.days_of_week } 
    it { assigns(:has_hit_staff_limit).should be_false 
    # etc .. 
    end 
end 

आशा कुछ इस मदद करता है।