Skip to content
Browse files

Improve pattern of testing driver against a rack app

  • Loading branch information...
1 parent 259a38f commit 265a7da6524bfdb3781223a79704d349988a41b8 @jferris jferris committed Jul 8, 2012
Showing with 435 additions and 437 deletions.
  1. +14 −16 spec/driver_rendering_spec.rb
  2. +365 −419 spec/driver_spec.rb
  3. +7 −2 spec/spec_helper.rb
  4. +49 −0 spec/support/app_runner.rb
View
30 spec/driver_rendering_spec.rb
@@ -3,14 +3,10 @@
require 'mini_magick'
describe Capybara::Webkit::Driver, "rendering an image" do
+ include AppRunner
- before(:all) do
- # Set up the tmp directory and file name
- tmp_dir = File.join(PROJECT_ROOT, 'tmp')
- FileUtils.mkdir_p tmp_dir
- @file_name = File.join(tmp_dir, 'render-test.png')
-
- app = lambda do |env|
+ let(:driver) do
+ driver_for_app do |env|
body = <<-HTML
<html>
<body>
@@ -22,22 +18,25 @@
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
[body]]
end
-
- @driver = Capybara::Webkit::Driver.new(app, :browser => $webkit_browser)
- @driver.visit("/hello/world?success=true")
end
- after(:all) { @driver.reset! }
+ before(:each) do
+ # Set up the tmp directory and file name
+ tmp_dir = File.join(PROJECT_ROOT, 'tmp')
+ FileUtils.mkdir_p tmp_dir
+ @file_name = File.join(tmp_dir, 'render-test.png')
+ driver.visit '/'
+ end
def render(options)
FileUtils.rm_f @file_name
- @driver.render @file_name, options
+ driver.render @file_name, options
@image = MiniMagick::Image.open @file_name
end
context "with default options" do
- before(:all){ render({}) }
+ before { render({}) }
it "should be a PNG" do
@image[:format].should == "PNG"
@@ -54,7 +53,7 @@ def render(options)
end
context "with dimensions set larger than necessary" do
- before(:all){ render(:width => 500, :height => 400) }
+ before { render(:width => 500, :height => 400) }
it "width should match the width given" do
@image[:width].should == 500
@@ -66,7 +65,7 @@ def render(options)
end
context "with dimensions set smaller than the document's default" do
- before(:all){ render(:width => 50, :height => 10) }
+ before { render(:width => 50, :height => 10) }
it "width should be greater than the width given" do
@image[:width].should > 50
@@ -76,5 +75,4 @@ def render(options)
@image[:height].should > 10
end
end
-
end
View
784 spec/driver_spec.rb
365 additions, 419 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
9 spec/spec_helper.rb
@@ -1,6 +1,7 @@
require 'rspec'
require 'rspec/autorun'
require 'rbconfig'
+require 'capybara'
PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')).freeze
@@ -20,12 +21,16 @@
c.filter_run_excluding :skip_on_windows => !(RbConfig::CONFIG['host_os'] =~ /mingw32/).nil?
end
-require File.join(spec_dir, "spec_helper")
-
require 'capybara/webkit'
connection = Capybara::Webkit::Connection.new(:socket_class => TCPSocket, :stdout => nil)
$webkit_browser = Capybara::Webkit::Browser.new(connection)
+RSpec.configure do |config|
+ config.before { $webkit_browser.reset! }
+end
+
+require File.join(spec_dir, "spec_helper")
+
Capybara.register_driver :reusable_webkit do |app|
Capybara::Webkit::Driver.new(app, :browser => $webkit_browser)
end
View
49 spec/support/app_runner.rb
@@ -0,0 +1,49 @@
+# Boots a single Capybara::Server for a Rack application that delegates to another, singleton Rack
+# application that can be configured for each spec. Also configures Capybara to use that server.
+
+module AppRunner
+ class << self
+ attr_accessor :app, :app_host
+ end
+
+ def self.boot
+ app_container = lambda { |env| AppRunner.app.call(env) }
+ server = Capybara::Server.new(app_container)
+ server.boot
+ self.app_host = "http://127.0.0.1:#{server.port}"
+ end
+
+ def self.reset
+ self.app = lambda do |env|
+ [200, { 'Content-Type' => 'html', 'Content-Length' => 0 }, []]
+ end
+ end
+
+ def run_application(&app)
+ AppRunner.app = app
+ end
+
+ def driver_for_app(&app)
+ run_application(&app)
+ Capybara::Webkit::Driver.new(app, :browser => $webkit_browser)
+ end
+
+ def self.included(example_group)
+ example_group.class_eval do
+ before { AppRunner.reset }
+
+ around do |example|
+ Capybara.run_server = false
+ Capybara.app_host = AppRunner.app_host
+ begin
+ example.run
+ ensure
+ Capybara.run_server = true
+ Capybara.app_host = nil
+ end
+ end
+ end
+ end
+end
+
+AppRunner.boot

0 comments on commit 265a7da

Please sign in to comment.
Something went wrong with that request. Please try again.