Improve pattern of testing driver against a rack app

This commit is contained in:
Joe Ferris 2012-07-08 13:38:19 -07:00
parent 259a38f206
commit 265a7da652
4 changed files with 435 additions and 437 deletions

View File

@ -3,14 +3,10 @@ require 'capybara/webkit/driver'
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 @@ describe Capybara::Webkit::Driver, "rendering an image" do
{ '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 @@ describe Capybara::Webkit::Driver, "rendering an image" do
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 @@ describe Capybara::Webkit::Driver, "rendering an image" do
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 @@ describe Capybara::Webkit::Driver, "rendering an image" do
@image[:height].should > 10
end
end
end

File diff suppressed because it is too large Load Diff

View File

@ -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 @@ RSpec.configure do |c|
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 File

@ -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