Skip to content
This repository
Browse code

Merge pull request #344 from haines/exorcise_poltergeist

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

0 comments on commit d2e4d21

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