2012-12-21 18 views
13

मैं विफल रहता है ...कैसे स्वचालित रूप से Rspec save_and_open_page बनाने के लिए जब किसी भी कल्पना

/spec/spec_helper.rb:

require 'capybara/rspec' 
require 'capybara/rails' 
require 'capybara/dsl' 

RSpec.configure do |config| 
    config.fail_fast = true 
    config.use_instantiated_fixtures = false 
    config.include(Capybara, :type => :integration) 
end 

तो जैसे ही किसी भी कल्पना विफल रहता है, Rspec इस्तीफा और शो आप त्रुटि

बिंदु पर, मैं चाहता हूं कि रुपेक स्वचालित रूप से कैपिबरा की save_and_open_page विधि को भी कॉल करे। मैं यह कैसे कर सकता हूँ?

Capybara-Screenshot आशाजनक लग रहा है, लेकिन जब यह एक HTML फ़ाइल और एक स्क्रीनशॉट को एक छवि फ़ाइल (जिसे मुझे आवश्यकता नहीं है) के रूप में सहेजता है, तो यह स्वचालित रूप से उन्हें नहीं खोलता है।

उत्तर

12

rspec की कॉन्फ़िगरेशन में आप प्रत्येक उदाहरण के लिए एक हुक (https://www.relishapp.com/rspec/rspec-core/v/2-2/docs/hooks/before-and-after-hooks) परिभाषित कर सकते हैं। यह बहुत अच्छी तरह से प्रलेखित नहीं है लेकिन इस हुक के लिए ब्लॉक example परम ले सकता है। example वस्तु पर आप परीक्षण कर सकते हैं: example.metadata[:type] == :feature

  • यह विफल हो गई है:

    • यह एक सुविधा कल्पना है example.exception.present?

    पूरा कोड कतरना तरह दिखना चाहिए:

    # RSpec 2 
        RSpec.configure do |config| 
        config.after do 
         if example.metadata[:type] == :feature and example.exception.present? 
         save_and_open_page 
         end 
        end 
        end 
    
        # RSpec 3 
        RSpec.configure do |config| 
        config.after do |example| 
         if example.metadata[:type] == :feature and example.exception.present? 
         save_and_open_page 
         end 
        end 
        end 
    
  • +0

    एक आकर्षण की तरह काम करता है ... धन्यवाद – rizidoro

    +1

    रुपेक 3 को काम करने के लिए 'डू | उदाहरण |' स्पष्ट रूप से रखना होगा – Eva

    1

    आरएसपीसी 2 में रेल 4 के संयोजन के साथ, मैं इस कॉन्फ़िगरेशन ब्लॉक का उपयोग करता हूं:

    # In spec/spec_helper.rb or spec/support/name_it_as_you_wish.rb 
    # 
    # Automatically save and open the page 
    # whenever an expectation is not met in a features spec 
    RSpec.configure do |config| 
        config.after(:each) do 
        if example.metadata[:type] == :feature and example.exception.present? 
         save_and_open_page 
        end 
        end 
    end