Skip to content

Commit

Permalink
Merge pull request #206 from oponder/front_end_tests_with_poltergeist
Browse files Browse the repository at this point in the history
Allow for front end tests with poltergeist
  • Loading branch information
gabceb committed Mar 13, 2013
2 parents 51837e1 + c592bef commit 6c2b3fe
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Expand Up @@ -57,6 +57,10 @@ group :test do
gem 'factory_girl_rails'
gem 'simplecov', :require => false
gem 'coveralls', :require => false

gem 'poltergeist'
gem 'launchy'
gem 'capybara'
end

group :development, :test do
Expand Down
18 changes: 18 additions & 0 deletions Gemfile.lock
Expand Up @@ -49,6 +49,13 @@ GEM
bourne (1.1.2)
mocha (= 0.10.5)
builder (3.0.4)
capybara (2.0.2)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0)
xpath (~> 1.0.0)
childprocess (0.3.9)
ffi (~> 1.0, >= 1.0.11)
climate_control (0.0.3)
Expand Down Expand Up @@ -153,6 +160,8 @@ GEM
activesupport (>= 3.0.0)
kandan-count (1.1.0)
kandan-count-dev (1.1.0)
launchy (2.1.2)
addressable (~> 2.3)
listen (0.7.3)
lumberjack (1.0.2)
mail (2.4.4)
Expand All @@ -174,6 +183,10 @@ GEM
cocaine (~> 0.5.0)
mime-types
pg (0.14.1)
poltergeist (1.1.0)
capybara (~> 2.0, >= 2.0.1)
faye-websocket (~> 0.4, >= 0.4.4)
http_parser.rb (~> 0.5.3)
polyglot (0.3.3)
pry (0.9.12)
coderay (~> 1.0.5)
Expand Down Expand Up @@ -269,6 +282,8 @@ GEM
warden (1.2.1)
rack (>= 1.0)
websocket (1.0.7)
xpath (1.0.0)
nokogiri (~> 1.3)
yajl-ruby (1.1.0)

PLATFORMS
Expand All @@ -281,6 +296,7 @@ DEPENDENCIES
binding_of_caller
bootstrap-sass (~> 2.3.0.1)
bourbon
capybara
coffee-rails
coveralls
database_cleaner
Expand All @@ -300,8 +316,10 @@ DEPENDENCIES
kaminari
kandan-count
kandan-count-dev
launchy
paperclip
pg
poltergeist
pry-rails
quiet_assets
rails (= 3.2.12)
Expand Down
@@ -1,7 +1,12 @@
class Kandan.Broadcasters.FayeBroadcaster

constructor: ()->
@fayeClient = new Faye.Client("/remote/faye")
@fayeClient = new Faye.Client("http://<%= ActionMailer::Base.default_url_options[:host] %>/remote/faye")

<% if Rails.env == "test" %>
@fayeClient.setHeader('Access-Control-Allow-Origin', '*');
<% end %>

@fayeClient.disable('websocket')
authExtension = {
outgoing: (message, callback)->
Expand Down
2 changes: 1 addition & 1 deletion app/assets/templates/chatbox.jst.eco
@@ -1,2 +1,2 @@
<textarea class="chat-input"></textarea>
<textarea class="chat-input" id="chat-input"></textarea>
<button class="post">Post</button>
3 changes: 2 additions & 1 deletion config/environments/development.rb
Expand Up @@ -39,5 +39,6 @@
config.logger.level = Logger.const_get(
ENV['LOG_LEVEL'] ? ENV['LOG_LEVEL'].upcase : 'DEBUG'
)


config.action_mailer.default_url_options = { :host => "localhost:3000" }
end
2 changes: 2 additions & 0 deletions config/environments/test.rb
Expand Up @@ -34,4 +34,6 @@

# Print deprecation notices to the stderr
config.active_support.deprecation = :stderr

config.action_mailer.default_url_options = { :host => "localhost:9292" }
end
2 changes: 1 addition & 1 deletion lib/broadcasters/faye.rb
Expand Up @@ -13,7 +13,7 @@ def broadcast(channel, message)
end

def assets
["/remote/faye.js"]
["http://#{ActionMailer::Base.default_url_options[:host]}/remote/faye.js"]
end
end
end
Expand Down
41 changes: 41 additions & 0 deletions spec/features/login_spec.rb
@@ -0,0 +1,41 @@
require 'spec_helper'

describe "Login" do
before do
@user = FactoryGirl.create(:user, :password => "mypassword")
@channel = FactoryGirl.create(:channel, :name => "Cool")
end

it "shows the login form when visiting the site", js: true do
visit root_path

page.should have_content("Sign In")

end

it "allows someone to log in and chat", js: true do
visit root_path
fill_in "Username", :with => @user.username
fill_in "Password", :with => "mypassword"

click_button "Sign in"

page.should have_content(@user.first_name)
page.should have_content(@user.last_name)

fill_in "chat-input", :with => "Hello there"
click_button "Post"

within("#channel-activities-1") do
page.should have_content("Hello there")
end

fill_in "chat-input", :with => "Hi again"
click_button "Post"

within("#channel-activities-1") do
page.should have_content("Hi again")
end

end
end
31 changes: 30 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -11,6 +11,35 @@
require 'rspec/autorun'
require "#{Rails.root}/lib/active_users.rb"

require 'capybara/rails'
require 'capybara/rspec'
require 'capybara/poltergeist'

Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {:timeout => 60})
end

Capybara.javascript_driver = :poltergeist

require 'faye'
require ::File.expand_path("../../lib/active_users.rb", __FILE__)
require ::File.expand_path("../../lib/faye_extensions/devise_auth.rb", __FILE__)

faye_server = Faye::RackAdapter.new(:mount => "/remote/faye", :timeout => 30)
faye_server.add_extension(DeviseAuth.new)

FAYE_CLIENT = faye_server.get_client

faye_server.bind(:unsubscribe) do |client_id|
ActiveUsers.remove_by_client_id(client_id)
end

Thread.new {faye_server.listen(9292)}

Capybara.app = Rack::URLMap.new({
"/" => Kandan::Application
})

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
Expand All @@ -29,7 +58,7 @@
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false

# Database cleaner
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
Expand Down
25 changes: 25 additions & 0 deletions spec/support/shared_db_connection.rb
@@ -0,0 +1,25 @@
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil

def self.connection
@@shared_connection || retrieve_connection
end
end

# Forces all threads to share the same connection. This works on
# Capybara because it starts the web server in a thread.
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection

if Rails.env.javascript_test?
module ActiveRecord
module ConnectionAdapters
module QueryCache
private
def cache_sql(sql,binds)
yield
end
end
end
end
end

0 comments on commit 6c2b3fe

Please sign in to comment.