Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support capybara/rspec better [Fixes #28]

  • Loading branch information...
commit 072a59ece2b30fd1455e4a4f8dea73eeabb85b3e 1 parent aa53c8d
@ConradIrwin authored
View
15 examples/capybara_spec.rb
@@ -0,0 +1,15 @@
+#!/usr/bin/env ruby
+require 'rspec'
+require 'rspec/autorun'
+
+$:.unshift File.expand_path '../../lib', __FILE__
+require 'pry-rescue/rspec'
+
+require 'capybara/rspec'
+
+describe "Google", :type => :feature, :driver => :selenium do
+ it "should make a nice bell-like sound" do
+ visit 'http://google.com/'
+ page.should have_content 'Bing'
+ end
+end
View
60 lib/pry-rescue/rspec.rb
@@ -1,12 +1,58 @@
require 'pry-rescue'
-RSpec.configure do |c|
- c.around(:each) do |example|
- Pry::rescue do
- example.binding.eval('@exception = nil')
- example.run
- if e = example.binding.eval('@exception')
- Pry::rescued(e)
+class PryRescue
+ class RSpec
+
+ # Run an Rspec example within Pry::rescue{ }.
+ #
+ # Takes care to ensure that `try-again` will work.
+ def self.run(example)
+ Pry::rescue do
+ begin
+ before
+
+ example.binding.eval('@exception = nil')
+ example.run
+ if e = example.binding.eval('@exception')
+ Pry::rescued(e)
+ end
+
+ ensure
+ after
+ end
+ end
+ end
+
+ def self.before
+ monkeypatch_capybara if defined?(Capybara)
+ end
+
+ def self.after
+ after_filters.each(&:call)
+ end
+
+ # Shunt Capybara's after filter from before Pry::rescued to after.
+ #
+ # The after filter navigates to 'about:blank', but people debugging
+ # tests probably want to see the page that failed.
+ def self.monkeypatch_capybara
+ unless Capybara.respond_to?(:reset_sessions_after_rescue!)
+ class << Capybara
+ alias_method :reset_sessions_after_rescue!, :reset_sessions!
+ def reset_sessions!; end
+ end
+
+ after_filters << Capybara.method(:reset_sessions_after_rescue!)
end
end
+
+ def self.after_filters
+ @after_filters ||= []
+ end
+ end
+end
+
+RSpec.configure do |c|
+ c.around(:each) do |example|
+ PryRescue::RSpec.run example
end
end
View
1  pry-rescue.gemspec
@@ -18,4 +18,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rspec'
s.add_development_dependency 'yard'
s.add_development_dependency 'redcarpet'
+ s.add_development_dependency 'capybara'
end
Please sign in to comment.
Something went wrong with that request. Please try again.