Browse files

Merge pull request #344 from haines/exorcise_poltergeist

Better integration specs
  • Loading branch information...
2 parents 970f3a3 + d98f623 commit d2e4d2165d5060c160d85a478de31d577c8652b8 @steveklabnik steveklabnik committed Nov 16, 2012
Showing with 78 additions and 56 deletions.
  1. +0 −5 .travis.yml
  2. +0 −6 ci/install_phantomjs
  3. +0 −1 draper.gemspec
  4. +19 −36 spec/integration/integration_spec.rb
  5. +9 −8 spec/support/dummy_app.rb
  6. +50 −0 spec/support/matchers/have_text.rb
View
5 .travis.yml
@@ -6,9 +6,4 @@ rvm:
- ruby-head
matrix:
allow_failures:
- - rvm: rbx-19mode
- rvm: ruby-head
-before_script:
- - sudo ci/install_phantomjs
- - "export PATH=phantomjs/bin:$PATH"
- - phantomjs --version
View
6 ci/install_phantomjs
@@ -1,6 +0,0 @@
-#!/bin/bash
-
-version=phantomjs-1.7.0-linux-i686
-wget http://phantomjs.googlecode.com/files/$version.tar.bz2
-tar xjf $version.tar.bz2
-mv $version phantomjs
View
1 draper.gemspec
@@ -26,5 +26,4 @@ Gem::Specification.new do |s|
s.add_development_dependency 'minitest-rails', '~> 0.2'
s.add_development_dependency 'minitest', '~> 3.0' if RUBY_PLATFORM == "java"
s.add_development_dependency 'capybara'
- s.add_development_dependency 'poltergeist'
end
View
55 spec/integration/integration_spec.rb
@@ -1,50 +1,33 @@
require 'spec_helper'
require 'support/dummy_app'
+require 'support/matchers/have_text'
-shared_examples_for "a decorator in a view" do
- it "works" do
- # it runs in the correct environment
- page.should have_css "#environment", text: environment
+app = DummyApp.new(ENV["RAILS_ENV"])
- # it can use path helpers with a model
- page.should have_css "#path_with_model", text: "/en/posts/1"
+app.start_server do
+ {view: "/posts/1", mailer: "/posts/1/mail"}.each do |type, path|
+ page = app.get(path)
- # it can use path helpers with an id
- page.should have_css "#path_with_id", text: "/en/posts/1"
-
- # it can use url helpers with a model
- page.should have_css "#url_with_model", text: "http://www.example.com/en/posts/1"
-
- # it can use url helpers with an id
- page.should have_css "#url_with_id", text: "http://www.example.com/en/posts/1"
- end
-end
-
-describe "integration" do
- include Capybara::DSL
-
- rails_env = ENV["RAILS_ENV"].to_s
- raise ArgumentError, "RAILS_ENV must be development or production" unless ["development", "production"].include?(rails_env)
-
- app = DummyApp.new(rails_env)
-
- app.start_server do
- describe "in #{rails_env}" do
- let(:environment) { rails_env }
- before { Capybara.app_host = app.url }
+ describe "in a #{type}" do
+ it "runs in the correct environment" do
+ page.should have_text(app.environment).in("#environment")
+ end
- context "in a view" do
- before { visit("/posts/1") }
+ it "can use path helpers with a model" do
+ page.should have_text("/en/posts/1").in("#path_with_model")
+ end
- it_behaves_like "a decorator in a view"
+ it "can use path helpers with an id" do
+ page.should have_text("/en/posts/1").in("#path_with_id")
end
- context "in a mailer" do
- before { visit("/posts/1/mail") }
+ it "can use url helpers with a model" do
+ page.should have_text("http://www.example.com/en/posts/1").in("#url_with_model")
+ end
- it_behaves_like "a decorator in a view"
+ it "can use url helpers with an id" do
+ page.should have_text("http://www.example.com/en/posts/1").in("#url_with_id")
end
end
end
-
end
View
17 spec/support/dummy_app.rb
@@ -1,25 +1,26 @@
-require 'capybara'
-require 'capybara/dsl'
-require 'capybara/poltergeist'
-require 'singleton'
require 'socket'
+require 'net/http'
# Adapted from code by Jon Leighton
# https://github.com/jonleighton/focused_controller/blob/ec7ccf1/test/acceptance/app_test.rb
-Capybara.run_server = false
-Capybara.default_driver = :poltergeist
-
class DummyApp
def initialize(environment)
+ raise ArgumentError, "Environment must be development or production" unless ["development", "production"].include?(environment.to_s)
@environment = environment
end
+ attr_reader :environment
+
def url
"http://#{localhost}:#{port}"
end
+ def get(path)
+ Net::HTTP.get(URI(url + path))
+ end
+
def within_app(&block)
Dir.chdir(root, &block)
end
@@ -66,7 +67,7 @@ def localhost
def port
@port ||= begin
server = TCPServer.new(localhost, 0)
- port = server.addr[1]
+ server.addr[1]
ensure
server.close if server
end
View
50 spec/support/matchers/have_text.rb
@@ -0,0 +1,50 @@
+require 'capybara'
+
+module HaveTextMatcher
+ def have_text(text)
+ HaveText.new(text)
+ end
+
+ class HaveText
+ def initialize(text)
+ @text = text
+ end
+
+ def in(css)
+ @css = css
+ self
+ end
+
+ def matches?(subject)
+ @subject = Capybara.string(subject)
+
+ @subject.has_css?(@css || "*", text: @text)
+ end
+
+ def failure_message_for_should
+ "expected to find #{@text.inspect} #{within}"
+ end
+
+ def failure_message_for_should_not
+ "expected not to find #{@text.inspect} #{within}"
+ end
+
+ private
+
+ def within
+ if @css && @subject.has_css?(@css)
+ "within\n#{@subject.find(@css).native}"
+ else
+ "#{inside} within\n#{@subject.native}"
+ end
+ end
+
+ def inside
+ @css ? "inside #{@css.inspect}" : "anywhere"
+ end
+ end
+end
+
+RSpec.configure do |config|
+ config.include HaveTextMatcher
+end

0 comments on commit d2e4d21

Please sign in to comment.