2010-06-15 14 views
16

कृपया, मेरी मदद करें। मैं उलझन में हूं। मैं कैसे मॉडल के राज्य-संचालित व्यवहार लिखने के लिए पता है, लेकिन मैं नहीं जानता कि क्या मैं चश्मा में लिखना चाहिए ...रेल: राज्य_माचिन का परीक्षण कैसे करें?

मेरे model.rb फ़ाइल देखने के

class Ratification < ActiveRecord::Base 
    belongs_to :user 

    attr_protected :status_events 

    state_machine :status, :initial => :boss do 
    state :boss 
    state :owner 
    state :declarant 
    state :done 

    event :approve do 
     transition :boss => :owner, :owner => :done 
    end 

    event :divert do 
     transition [:boss, :owner] => :declarant 
    end 

    event :repeat do 
     transition :declarant => :boss 
    end 

    end 
end 

मैं state_machine मणि का उपयोग करें।

कृपया मुझे पाठ्यक्रम दिखाएं।

+0

डेटाबेस को मारने के बिना ऐसा करने का कोई तरीका? ऐसा लगता है कि हम डेटाबेस को हिट करने के लिए जो भी राज्य_माचिन उपयोग कर रहे हैं उसे स्टब करने में सक्षम होना चाहिए और फिर भी हमारे द्वारा अपेक्षित सभी परिवर्तन प्राप्त करें। –

उत्तर

0

दुर्भाग्यवश, मुझे लगता है कि आपको प्रत्येक राज्य -> ​​राज्य संक्रमण के लिए एक परीक्षण करने की आवश्यकता है, जो कोड डुप्लिकेशन की तरह महसूस हो सकता है।

describe Ratification do 
    it "should initialize to :boss" do 
    r = Ratification.new 
    r.boss?.should == true 
    end 

    it "should move from :boss to :owner to :done as it's approved" do 
    r = Ratification.new 
    r.boss?.should == true 
    r.approve 
    r.owner?.should == true 
    r.approve 
    r.done?.should == true 
    end 

    # ... 
end 

सौभाग्य से, मुझे लगता है कि यह आमतौर पर एकीकरण परीक्षण में फिट बैठता है। उदाहरण के लिए, एक भुगतान प्रणाली के लिए एक अत्यंत सरल राज्य मशीन होगा:

class Bill < ActiveRecord::Base 
    belongs_to :account 

    attr_protected :status_events 

    state_machine :status, :initial => :unpaid do 
    state :unpaid 
    state :paid 

    event :mark_as_paid do 
     transition :unpaid => :paid 
    end 
    end 
end 

तुम अब भी ऊपर के रूप में इकाई परीक्षण हो सकता है, लेकिन आप शायद भी एकीकरण परीक्षण करना होगा, कुछ की तरह:

describe Account do 
    it "should mark the most recent bill as paid" do 
    @account.recent_bill.unpaid?.should == true 
    @account.process_creditcard(@credit_card) 
    @account.recent_bill.paid?.should == true 
    end 
end 

वह बहुत सारी हैंडवाइविंग था, लेकिन उम्मीद है कि यह समझ में आता है। मैं भी आरएसपीसी के लिए सुपर इस्तेमाल नहीं कर रहा हूं, इसलिए उम्मीद है कि मैंने वहां बहुत सारी गलतियां नहीं की हैं। यदि इसका परीक्षण करने के लिए एक और शानदार तरीका है, तो मुझे अभी तक यह नहीं मिला है।

+2

आप इसका उपयोग कर सकते हैं ... @ account.recent_bill.should be_unpaid –

9

सवाल पुराना है, लेकिन मेरे पास वही था। state_machine gem से उदाहरण लें:

class Vehicle 
    state_machine :state, :initial => :parked do 
    event :park do 
     transition [:idling, :first_gear] => :parked 
    end 

    event :ignite do 
     transition :stalled => same, :parked => :idling 
    end 

    event :idle do 
     transition :first_gear => :idling 
    end 

    event :shift_up do 
     transition :idling => :first_gear, :first_gear => :second_gear, :second_gear => :third_gear 
    end 

    event :shift_down do 
     transition :third_gear => :second_gear, :second_gear => :first_gear 
    end 
    end 
end 

मेरे समाधान किया गया था:

describe Vehicle do 

    before :each do 
    @vehicle = Factory(:vehicle) 
    end 

    describe 'states' do 
    describe ':parked' do 
     it 'should be an initial state' do 
     # Check for @vehicle.parked? to be true 
     @vehicle.should be_parked 
     end 

     it 'should change to :idling on :ignite' do 
     @vehicle.ignite! 
     @vehicle.should be_idling 
     end 

     ['shift_up!', 'shift_down!'].each do |action| 
     it "should raise an error for #{action}" do 
      lambda {@job_offer.send(action)}.should raise_error 
     end 
     end 
    end 
    end 
end 

मैं उपयोग कर रहा था:

  • माणिक (1.9.3)
  • रेल (3.1.3)
  • rspec (2.8.0.rc1)
  • factory_g IRL (2.3.2)
  • state_machine (1.1.0)
1

मैं एक RSpec कस्टम मिलान लिखा है। यह सुरुचिपूर्ण और सरल तरीके से राज्य प्रवाह का परीक्षण करने की अनुमति देता है: check it out

2

state_machine_rspec मणि में संक्षिप्त चश्मा लिखने के लिए कई सहायक विधियां शामिल हैं।

describe Ratification do 
    it { should have_states :boss, :declarant, :done, :owner } 
    it { should handle_events :approve, when: :boss } 
    it { should handle_events :approve, when: :owner } 
    it { should handle_events :divert, when: :boss } 
    it { should handle_events :divert, when: :owner } 
    it { should handle_events :repeat, when: :declarant } 
    it { should reject_events :approve, :divert, :repeat, when: :done } 
    it { should reject_events :approve, :divert, :repeat, when: :done } 
end 

ये RSpec matchers एक उच्चस्तरीय से state_machine चश्मा के साथ मदद करेंगे। यहां से, can_approve?, can_divert?, और can_repeat? के लिए व्यावसायिक मामलों के लिए चश्मा लिखने की आवश्यकता है।

संबंधित मुद्दे