Capybara visit not working - selenium

ok I not sure what I am missing on the setting up of capybara, but on testing the visit link would appear not to be working. so I have a 3.2 app, I have my specs_helper set up like so:
require "spork"
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'factory_girl'
require 'capybara/rspec'
require 'capybara/rails'
Capybara.javascript_driver = :selenium
Capybara.default_wait_time = 5
include Devise::TestHelpers # see spec/support/devise.rb
include CarrierWave::Test::Matchers
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
include New_spec_helpers
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.before :each do
Capybara.default_selector = :css
if Capybara.current_driver == :rack_test
DatabaseCleaner.strategy = :transaction
else
DatabaseCleaner.strategy = :truncation
end
DatabaseCleaner.start
end
config.after do
DatabaseCleaner.clean
end
end
end
Spork.each_run do
FactoryGirl.factories.clear
FactoryGirl.define do
sequence(:code) { |n| "code#{n}" }
sequence(:title) { |n| "title#{n}" }
end
Dir[Rails.root.join("spec/factories/**/*.rb")].each{|f| load f}
PortfolioNew::Application.reload_routes!
end
Then my test set up like this:
before(:each) do
build_projects
visit(projects_path)
end
it "test js" , :js=>true do
current_path.should == projects_path
end
However all i get in return is this:
Failure/Error: current_path.should == projects_path
expected: "/projects"
got: nil (using ==)
i have checked my routes and the projects path is definitely correct and working. Any thoughts on what the issue is would be gratefully received

Ok I found the issue in a comment on the this similar post Rails 3 rspec + capybara - current_path is nil?
Turns out the webrat conflicts with Capybara which was left in when from old tests.
So for anyone interested I just commented out of my gemfile and all good!

Try moving the visit call into the it block:
before(:each) do
build_projects
end
it "test js" , :js=>true do
visit(projects_path)
current_path.should == projects_path
end

Related

Can't get any FactoryGirl factories to register

I'm trying to set up Rspec and FactoryGirl on an existing Rails 3 project which previously has had no automated testing.
The error I'm getting when running the test as below is Factory not registered: admin
I can't see why this would be failing, it happens for every test through my specs that use a factory.
/Gemfile.rb (concatenated for brevity)
group :development, :test do
gem 'rspec-rails'
gem 'factory_girl_rails
end
/config/application.rb (concatenated for brevity)
config.generators do |g|
g.test_framework :rspec, fixture: true
g.fixture_replacement :factory_girl, dir: "spec/factories"
end
/spec/spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/mechanize'
require 'factory_girl_rails'
HTTPI.log = false
FactoryGirl.factories.clear
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.expect_with :rspec do |c|
c.syntax = :expect
end
config.include Devise::TestHelpers, type: :controller
config.extend ControllerMacros, type: :controller
end
/spec/support/controller_macros.rb
module ControllerMacros
def login_admin
before(:each) do
#request.env["devise.mapping"] = Devise.mappings[:admin]
sign_in FactoryGirl.create(:admin)
end
end
end
/spec/factories/users.rb
FactoryGirl.define do
factory :admin do
sequence(:email) {|n| "user#{n}#local.test"}
password "password"
password_confirmation "password"
association :user_role, factory: :admin_role
trait :as_reseller do
association :user_role, factory: :reseller_role
end
trait :as_customer do
association :user_role, factory: :customer_role
end
end
factory :reseller, parent: :admin do
as_reseller
end
factory :customer, parent: :admin do
as_customer
end
end
/spec/controllers/accounts_controller_spec.rb
describe AccountCodesController do
describe "as administrator" do
login_admin
describe "GET 'index'" do
it "returns http success" do
get 'index'
response.should be_success
end
end
end
end
This line FactoryGirl.factories.clear confuse me a lot.
According to doc you needn't do any spec_helper modification (if you use it in Rails app and through Bundle).
This is my spec_helper in one of my APP and I use factories in it successfully.
It's explain on the link
The stable documentation is here :
http://github.com/thoughtbot/factory_girl/tree/1.3.x

Rspec headaches: configuration advice appreciated

I can't for the life of me seem to set up rspec/capybara in a way that doesn't throw random errors from time to time. My most common errors (but not consistent) are along the lines of:
An error occurred in an after hook
ActiveRecord::RecordNotFound: Couldn't find Post with id=1
occurred at [...]/gems/activerecord-3.2.11/lib/active_record/relation/finder_methods.rb:341:in `find_one'
And a close second is anything related to expecting model objects that were created within the spec and then not found later. e.g. something like
Sorter should populate featured_posts with appropriate posts
Failure/Error: Sorter.top_4.map(&:id).should eq(([best_new_post, best_old_post] + new_posts.last(2).reverse).map(&:id))
expected: [2, 1, 40, 38]
got: []
These tests will always pass when I run them individually, and they don't always fail when I run them in the suite. I'm assuming there is some sort of issue with threads and how the database is set up, but I can't seem to figure it out.
My spec_helper.rb is a hodgepodge of advice from various sources:
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/email/rspec'
include ApplicationHelper
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
include Warden::Test::Helpers
Warden.test_mode!
ActionController::Base.asset_host = "http://localhost:3000"
RSpec.configure do |config|
config.include Rails.application.routes.url_helpers
config.use_transactional_fixtures = false
config.use_transactional_examples = false #factoryGirl
# From http://railsapps.github.io/tutorial-rails-devise-rspec-cucumber.html
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) { DatabaseCleaner.start }
config.after(:each) { DatabaseCleaner.clean }
#
config.infer_base_class_for_anonymous_controllers = false
## Setting up email helpers
config.include MailerMacros
config.before(:each) { reset_email }
## Setting up test and login helpers for Devise testing
config.include Devise::TestHelpers, type: :controller
config.extend ControllerMacros, type: :controller
# From http://railscasts.com/episodes/413-fast-tests
config.order = "random"
config.include FactoryGirl::Syntax::Methods
config.treat_symbols_as_metadata_keys_with_true_values = true
config.filter_run focus: true
config.run_all_when_everything_filtered = true
config.filter_run_excluding :slow unless ENV["SLOW_SPECS"]
config.before(:all) { DeferredGarbageCollection.start }
config.after(:all) { DeferredGarbageCollection.reconsider }
end
end
Spork.each_run do
Rails.cache.clear
Warden.test_reset!
FactoryGirl.reload
end
I've tried removing pieces to troubleshoot, but it's hard to gauge whether it hss solved anything since the errors are sporadic.
Is there anything here that stands out as an obvious reason for the errors above, or any advice on how to troubleshoot this?
In your spec_helper.rb, try setting
config.use_transactional_fixtures = true

Capybara actions not affecting test database

I have an app with users who can post and also vote for other posts. In testing with Capybara, I am having trouble getting the right actions to come through.
I have simplified my tests as much as possible to try to find the error easily, to no avail:
requests/users_spec.rb
require 'spec_helper'
describe "Users" do
before :all do
Warden.test_reset!
Warden.test_mode!
end
describe "when logged in, CAN" do
before :all do
User.all.map(&:destroy)
Post.all.map(&:destroy)
#user = User.create!(email: "foo#bar.baz",
username: "foobar",
password: "foobar",
password_confirmation: "foobar",
confirmed_at: Time.now - 1.minute)
#user2 = User.create!(email: "foo2#bar.baz",
username: "foobar2",
password: "foobar",
password_confirmation: "foobar",
confirmed_at: Time.now - 1.minute)
#post = Post.create!(
title: Faker::Lorem.sentence(5),
content: Faker::Lorem.sentence(25),
post_type: "temporary",
user_id: #user2.id
)
vote = LoggedUpvote.create!(user_id: #user2.id, post_id: #post.id)
login_as(#user, scope: :user)
end
it "create Upvotes", js: true do
count = Upvotes.all.count
visit post_path(#post.obfuscated_id)
page.find('.upvote').click
Upvotes.all.count.should eq(count + 1)
end
end
end
In my spec_helper.rb file I have:
Capybara.run_server = true
Capybara.server_port = 7000
Capybara.app_host = "http://localhost:#{Capybara.server_port}"
Capybara logs in just fine, navigates fine, and performs the actions fine, but the database record for the upvote is not created (despite showing visual confirmation that it has been created and that the right user is logged in).
The upvote is submitted through ajax, and a success response is shown on the screen, but no record is created in the database.
How can I get the record to create?
EDIT My spec_helper file looks like:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/rails'
require 'rubygems'
require 'spork'
include Warden::Test::Helpers
# From http://stackoverflow.com/questions/8662554/how-to-use-the-test-database-with-capybara
Capybara.run_server = true
Capybara.server_port = 7000
Capybara.app_host = "http://localhost:#{Capybara.server_port}"
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
Spork.prefork do
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.infer_base_class_for_anonymous_controllers = false
config.order = "random" # From http://railscasts.com/episodes/413-fast-tests
config.include FactoryGirl::Syntax::Methods
config.treat_symbols_as_metadata_keys_with_true_values = true
config.filter_run focus: true
config.run_all_when_everything_filtered = true
config.filter_run_excluding :slow unless ENV["SLOW_SPECS"]
config.before(:all) { DeferredGarbageCollection.start }
config.after(:all) { DeferredGarbageCollection.reconsider }
config.before(:suite) { DatabaseCleaner.strategy = :truncation }
config.before(:each) { DatabaseCleaner.start }
config.after(:each) { DatabaseCleaner.clean }
end
end
Spork.each_run do
FactoryGirl.reload
end
I'm assuming that you are using a driver other than RackTest (based on your mention of ajax, you'd need some sort of javascript support). If so, you may not have setup Capybara correctly to share a database with the selenium driver. See this SO answer, this blog post or this part of the README for more details.
As it turns out, the database setup was fine, but Capybara was not waiting properly for the AJAX transaction to finish.
The solution was to add the following after the Upvote click and before the test assertion:
require "timeout"
Timeout.timeout(Capybara.default_wait_time) do
sleep(0.1) until Upvote.all.count == count + 1
end
And now everything works great.
Source: https://gist.github.com/jnicklas/d8da686061f0a59ffdf7

Setting HTTP_REFERER does not work in before hook of RSpec config

I saw various versions of how to setup a global HTTP_REFERER in RSpec, but none of them worked with RSpec 2.6.4:
RSpec.configure do |config|
config.before(:each, :type => :controller) do
request.env["HTTP_REFERER"] = root_url
end
end
The request is always nil:
undefined method `env' for nil:NilClass
RSpec is calling this:
def self.eval_before_eachs(example)
world.run_hook_filtered(:before, :each, self, example.example_group_instance, example)
ancestors.reverse.each { |ancestor| ancestor.run_hook(:before, :each, example.example_group_instance) }
end
# spec/support/http_referer.rb
module HttpReferer
def self.included(base)
base.class_eval do
setup :setup_http_referer if respond_to?(:setup)
end
end
def setup_http_referer
#request.env["HTTP_REFERER"] = "/back"
end
end
# spec/spec_helper.rb
RSpec.configure do |config|
config.include HttpReferer, :type => :controller
end
This link may help get you pointed in the right direction...
https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-%28and-rspec%29
You'll need to make a module like...
module Foo
def set_referer
#request.env["HTTP_REFERER"] = root_url
end
end
Then configure RSpec...
RSpec.configure do |config|
config.extend Foo, :type => :controller
end
Then call it in each of your controller specs...
describe MyController do
set_referer
end
We're using a similar approach to set our session cookie, but YMMV.

Just upgrading capybara from 0.3.9 to 0.4.1.2, cukes and specs fail

Using capybara (0.3.9) with steak and when running my steak acceptance spec with :js => true, firefox is fired and it works.
But when I upgrade capybara to 0.4.1.2 in Gemfile and run the same spec, firefox is fired on port 9887 but the page just show Unable to connect.
I think the selenium server couldn't fire.
This also affects my cucumber scenarios with #javascript tag too.
Following is my spec_helper.rb
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
...
RSpec.configure do |config|
...
config.before(:each, :type => :acceptance) do
Capybara.reset_sessions!
Capybara.default_selector = :css
Capybara.default_driver = :selenium
Capybara.javascript_driver = :selenium
Capybara.current_driver = :selenium if example.metadata[:js]
end
config.after(:each, :type => :acceptance) do
Capybara.use_default_driver if example.metadata[:js]
end
end
end
Spork.each_run do
load "#{Rails.root}/spec/Sporkfile.rb" if File.exists?("#{Rails.root}/spec/Sporkfile.rb")
end
and this is the acceptance_helper.rb
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
require "steak"
# Put your acceptance spec helpers inside /spec/acceptance/support
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}