Spent whole day with the problem.
Very simple test. I want to visit main page and check if it has text "Match".
require 'spec_helper'
describe "Sign in" do
it "should sign in with correct data", :js => true do
visit root_path
page.should have_content("Match")
end
end
Running rake spec:requests I've got:
1) Sign in should sign in with correct data
Failure/Error: visit root_path
Timeout::Error:
Timeout::Error
# ./spec/requests/signin_spec.rb:5:in `block (2 levels) in <top (required)>'
Can't fix it. It doesn't work with or without spork and guard, and making me crazy :(
My environment:
Gemfile:
group :test, :development do
gem 'rspec-rails', '2.7.0' , :branch => 'rails3'
gem 'capybara'
gem 'guard'
gem 'guard-rspec'
gem 'guard-spork'
gem 'growl'
gem 'launchy'
gem 'growl_notify', :require => false if RUBY_PLATFORM =~/darwin/i
gem 'rb-fsevent', :require => false if RUBY_PLATFORM =~/darwin/i
end
spec_helper.rb:
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# Add this to load Capybara integration:
require 'capybara/rspec'
require 'capybara/rails'
# 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}
RSpec.configure do |config|
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
#config.treat_symbols_as_metadata_keys_with_true_values = true
#config.filter_run :focus => true
#config.run_all_when_everything_filtered = true
end
end
Spork.each_run do
# This code will be run each time you run your specs.
FactoryGirl.reload
end
Related
I am trying to create a test for Login; The functionality has been implemented and working fine but I am trying to create a test for it. However, User model is empty in sessions controller but it has content in the test code. On the code below, I added binding.pry(debugger) to check for the User model.
It's working fine too when I am not using webdriver(js: true), if it's just purely like this:
post "/api/user/sign_in", {
user: {
email: user.email,
password: user.password
}
}
expect(response).to be_success
I've been using the same test code before which is working fine. I am not just sure if it has something to do with my front-end is AngularJS.
TEST CODE
require 'rails_helper'
describe Model, js: true do
let(:model) { Model }
let!(:user) { create(:user) }
it "Login" do
binding.pry ### #user exists in User.all #####
visit root_path
find('#btn-signin').click
find('#tab-signin').click
fill_in 'user_email', with: user.email
fill_in 'user_password', with: user.password
find('#btn-login').click
binding.pry #### user exists in User.all ####
expect(page).to have_css '.profile-pic'
end
end
SESSIONS CONTROLLER
def create
puts User.pluck(:email).join(', ') #### user does not exists (it's actually blank) ####
warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
render :json => current_user.to_json
end
GEMS
group :development, :test do
gem 'factory_girl_rails'
gem 'pry'
gem 'quiet_assets'
gem 'rspec-rails', '~> 3.0.0'
end
group :test do
gem 'codeclimate-test-reporter', require: nil
gem 'shoulda-matchers'
gem 'jasmine'
gem 'capybara'
gem 'selenium-webdriver', '~> 2.38.0'
end
RAILS SPEC
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include Capybara::DSL
config.include FactoryGirl::Syntax::Methods
config.use_transactional_fixtures = true
config.include UserMacros
config.infer_spec_type_from_file_location!
end
SOLUTION:
GEMFILE
gem 'database_cleaner'
RAILS_HELPER.RB
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
I followed the railscasts about using carrierwave with fog with aws s3 to the T but it doesnt seem to work.
The Gemfile is as follows:
source 'https://rubygems.org'
ruby '1.9.3'
gem 'rails', '3.2.13'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'pg'
gem 'pg_search'
gem 'devise'
gem "therubyracer"
gem "less-rails" #Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
gem "twitter-bootstrap-rails", :git => 'git://github.com/seyhunak/twitter-bootstrap-rails.git'
gem "simple_form"
gem 'aws-sdk', '~> 1.8.1.2'
gem 'activerecord-reputation-system', require: 'reputation_system'
gem 'bootstrap-will_paginate'
gem 'carrierwave'
gem "rmagick"
gem "fog", "~> 1.3.1"
gem 'carrierwave-aws'
config/initializers/carrierwave.rb
require 'carrierwave'
CarrierWave.configure do |config|
config.fog_credentials = {
provider: "AWS",
aws_access_key_id: ENV["xxx"],
aws_secret_access_key: ENV["yyy"],
region: ENV['ap-southeast-1']
}
config.cache_dir = "#{Rails.root}/tmp/uploads"
config.fog_directory = ENV["ABC"]
end
The uploader:
class ImageUploader < CarrierWave::Uploader::Base
require 'carrierwave/processing/mime_types'
before :store, :remember_cache_id
after :store, :delete_tmp_dir
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
# Choose what kind of storage to use for this uploader:
# storage :file
storage :fog
include CarrierWave::MimeTypes
process :set_content_type
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :resize_to_limit => [200, 200]
end
# store! nil's the cache_id after it finishes so we need to remember it for deletion
def remember_cache_id(new_file)
#cache_id_was = cache_id
end
def delete_tmp_dir(new_file)
# make sure we don't delete other things accidentally by checking the name pattern
if #cache_id_was.present? && #cache_id_was =~ /\A[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}\z/
FileUtils.rm_rf(File.join(root, cache_dir, #cache_id_was))
end
end
end
the controller:
class NotesController < ApplicationController
before_filter :authenticate_user!, only: [:create, :destroy]
before_filter :correct_user, only: :destroy
def index
end
def create
#note = current_user.notes.build(params[:note])
#notes = Note.paginate(page: params[:page])
if #note.save
flash[:success] = "Note uploaded!"
redirect_to root_url
else
flash.now[:error] = "Note failed to upload."
render 'static_pages/home'
end
end
def destroy
#note.destroy
flash[:success] = "Note deleted."
redirect_to root_url
end
private
def correct_user
#note = current_user.notes.find_by_id(params[:id])
redirect_to root_url if #note.nil?
end
end
I don't really know if the problem lies in the gem or the controller. Even the controller is based on the microposts controller in Michael Hartl's book "The Ruby on Rails Tutorial" so I don't know where I have gone wrong.
That's an awfully early version of fog. The latest is 1.12.1. Try updating to the latest release using the following entry in your Gemfile
gem "fog", "~> 1.12.1"
I'm using Authlogic and having difficulty getting my Selenium tests to work with :js=>true after upgrading to Rails 3.2.12. The same tests use to worked under Rails 3.1.3. I am running my tests using Spork.
I am logging in through Selenium launches Firefox (19.0.2), fills the login form but then I get a permissions error from Authlogic, the standard "You are not allowed to access this action."
I can see that many people are having issues with this but as I mentioned, has only became a problem for me once upgrading from Rails 3.1.3 to Rails 3.2.12. I suspect that the issue may be within my spec_helper file (below) and in particularly within the
module Authlogic
block which I got from here:
Authlogic with Capybara + Cucumber + Selenium Driver not working
**spec_helper.rb**
require 'rubygems'
require 'spork'
require 'authlogic/test_case'
include Authlogic::TestCase
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.color_enabled = true
ApplicationController.skip_before_filter :activate_authlogic
config.before(:each, :type => :request) do
activate_authlogic
end
config.include FactoryGirl::Syntax::Methods
config.include Capybara::DSL
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
Capybara.current_driver = :selenium
DatabaseCleaner.strategy = :truncation
module Authlogic
module Session
module Activation
module ClassMethods
def controller
if !Thread.current[:authlogic_controller]
Thread.current[:authlogic_controller] = Authlogic::TestCase::MockController.new
end
Thread.current[:authlogic_controller]
end
end
end
end
end
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.include(MailerMacros)
config.before(:each) { reset_email }
config.mock_with :mocha
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.treat_symbols_as_metadata_keys_with_true_values = true
config.filter_run :focus => true
config.run_all_when_everything_filtered = true
end
end
Spork.each_run do
FactoryGirl.reload
end
My understanding is that Authlogic and Selenium Webdriver work on different threads hence the need for this patch in the spec_helper file.
In the Request Spec I get a permissions error from Authlogic. Here is the Request Spec test in question:
# UNIT REQUEST SPEC
require 'spec_helper'
describe "Units" do
describe "GET /admin/units/new" do
before(:each) do
activate_authlogic
UserSession.create FactoryGirl.create(:admin_user, :email => "foo#bar.com", :password => "password", :password_confirmation => "password")
visit root_path
fill_in "user_session[email]", :with => "foo#bar.com"
fill_in "user_session[password]", :with => "password"
click_button "Sign In"
end
it "displays a pop up dialog after unit is created", :focus, :js => true do
visit new_admin_unit_path
fill_in "Title", :with => "Unit Title"
fill_in "Code", :with => "U-TEST"
fill_in "Learning Outcome", :with => "Some Learning Outcome"
fill_in "unit[learning_outcomes_attributes][0][assessment_methods_attributes][0][content]", :with => "Some Assessment Criteria"
click_button "Save and Publish"
page.should have_css('div.ui-dialog')
end
end
end
My Capybara tests are working ok with:
activate_authlogic
UserSession.create FactoryGirl.build(:user)
in the before(:each) block, the problem only arises when using :js=>true
So, my questions are:
Is it a case that with Rails 3.2.x that the spec_helper fix (module Authlogic ... ) no longer works.
If that is in fact the case, what is the "normal" way of getting this to work?
Just revisited this today. When I upgraded Rails I also upgraded capybara to the latest version (2.1.0). It seems that downgrading capybara from this version (2.1.0) to version 2.0.1 seemed to do the trick. I can see that capybara (2.0.1) uses xpath (1.0.0) while capybara (2.1.0) uses xpath (2.0.0) along with a different version of nokogiri gem. I have no idea if there is a link there but here is my gem file if anyone cares to take a look:
source 'http://rubygems.org'
gem 'rails', '3.2.12'
gem 'rack-mini-profiler'
gem 'authlogic'
gem 'rails3-generators'
gem 'mysql2'
gem 'declarative_authorization'
gem 'kaminari'
gem "foreigner"
gem 'validates_timeliness'
gem "activerecord-import", ">= 0.2.0"
gem 'ezcrypto'
gem 'thin'
gem 'exception_notification'
gem 'mail'
gem 'libv8', '~> 3.11.8.0'
gem "therubyracer", :require => 'v8'
gem 'jasmine', :group => [:development, :test]
gem 'sprockets'
gem 'jquery-rails'
gem 'tinymce-rails'
gem 'acts_as_list'
gem 'cocaine'
gem 'rmagick'
gem 'carrierwave'
gem 'remotipart'
gem 'deep_cloneable', '~> 1.4.0'
gem 'delayed_job_active_record'
gem 'daemons'
gem 'rb-readline'
gem "jquery-migrate-rails", "~> 1.0.0"
gem 'ransack'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', "~> 3.2.3"
gem 'coffee-rails', "~> 3.2.1"
gem 'uglifier', '>= 1.0.3'
end
# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
group :development, :test do
gem 'cheat'
gem 'ruby-growl'
gem 'letter_opener'
gem 'rspec-rails', "~> 2.0"
gem 'database_cleaner'
gem 'rb-fsevent', '~> 0.9.1'
gem 'guard-rspec'
gem 'spork-rails'
gem 'guard-spork'
gem 'factory_girl_rails'
gem 'capybara', '2.0.1'
gem 'launchy'
gem 'mocha', :require => false
gem 'better_errors'
gem 'binding_of_caller'
gem 'meta_request'
end
I'm using this solution to avoid issues with the database during javascript tests.
The first run through the suite, the tests run fine, all passing.
If I run the entire suite again, they'll still pass.
But, if I run an individual spec file and then try to run the suite (or other individual test), I get this error:
An error occurred in an after hook
ActiveRecord::StatementInvalid: ArgumentError: prepare called on a closed database: rollback transaction
occurred at /home/steveq/.rvm/gems/ruby-1.9.3-p194#rails32/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `initialize'
1) Signing up with valid information
Failure/Error: visit "/sign_up"
ActiveRecord::StatementInvalid:
ArgumentError: prepare called on a closed database: PRAGMA table_info("users")
# ./app/controllers/registrations_controller.rb:3:in `new'
# ./app/controllers/registrations_controller.rb:3:in `new'
# ./spec/features/sign_up_feature_spec.rb:5:in `block (2 levels) in <top (required)>'
If I reload guard, the tests will pass again.
Does anyone have any insight into what's happening here or any possible solutions? I've tried every variation I can think of, and here's my spec_helper file to show the things I've tried (the variations are commented out, the current code - what's suggested on the capybara page - is what I'm using now).
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# 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}
RSpec.configure do |config|
#Make it so Selenium (out of thread) tests can work with transactional fixtures
#REF http://opinionated-programmer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/#comment-220
# ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do
# def current_connection_id
# # Thread.current.object_id
# Thread.main.object_id
# end
# end
# FactoryGirl short syntax
config.include FactoryGirl::Syntax::Methods
config.use_transactional_fixtures = true
# set up for use with :js => true.
# See http://stackoverflow.com/questions/8178120/capybara-with-js-true-causes-test-to-fail for more info
# config.before :suite do
# if Capybara.current_driver == :rack_test
# DatabaseCleaner.strategy = :transaction
# else
# DatabaseCleaner.strategy = :truncation
# end
# DatabaseCleaner.start
# end
# config.after do
# DatabaseCleaner.clean
# end
# standard RSPEC config
# config.before(:suite) :truncation
# else
# :transaction
# end do
# DatabaseCleaner.strategy = if example.metadata[:js]
# :truncation
# else
# :transaction
# end
# DatabaseCleaner.clean_with(:truncation)
# end
# config.before(:each) do
# DatabaseCleaner.start
# end
# config.after(:each) do
# DatabaseCleaner.clean
# end
# config.before(:each) do
# DatabaseCleaner.strategy = if example.metadata[:js]
# :truncation
# else
# :transaction
# end
# DatabaseCleaner.start
# end
# config.after(:each) do
# DatabaseCleaner.clean
# end
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.treat_symbols_as_metadata_keys_with_true_values = true
config.filter_run :focus => true
config.run_all_when_everything_filtered = true
config.include MailerMacros
config.include LoginMacros
config.before(:each) { reset_email }
config.include Devise::TestHelpers, :type => :controller
config.extend LoginMacros, :type => :controller
end
end
Spork.each_run do
# allows capybara JS tests to run in separate thread
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
# This code will be run each time you run your specs.
load "#{Rails.root}/config/routes.rb"
FactoryGirl.reload
# reload all the models
Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
load model
end
end
So here's what I figured out - hopefully it will help anyone else who gets into this same trouble.
Firstly, the method I was using:
Spork.each_run do
# allows capybara JS tests to run in separate thread
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
# This code will be run each time you run your specs.
load "#{Rails.root}/config/routes.rb"
FactoryGirl.reload
# reload all the models
Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
load model
end
end
works just fine, but not, it seems, with sqlite.
The quickest fix was simply to swap out sqlite as my test db for mysql. That solved everything.
Another solution, one that I'm really digging right now, was to drop spork completely in favor of Zeus.
You can check it out via the github link, but I'll tell you why I like it.
It has no necessary config - no spork block in the spec_helper, no spork block in the guardfile.
It also speeds up server and console initialization to under a second- not a huge deal, but very, very pleasant.
My test suite (191 examples so far) went from running in around 35 seconds to 17.5 seconds - half the time.
I urge you to check it out.
I just started some tests on my Rails application in which I use Devise for authentication. I just generated rspec and only added
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
to my spec/support/devise.rb file.
I also added Capybara to the test group in my gemfile, run the bundle command, etc. But as I do visit in my tests, I get a no method error. Here an example of my user_spec.rb:
require 'spec_helper'
describe "Club" do
before(:each) do
#club ||= FactoryGirl.create(:club)
FactoryGirl.create(:membership)
User.all.each{|x| x.delete}
end
describe "privacy" do
it "shouldn't be shown any tournament if the user is private" do
attributes = FactoryGirl.attributes_for(:user)
user = User.create!({private: true}.merge(attributes))
assert(user.private)
visit "/user/#{user.id}/tournaments"
page.should have_no_selector(".tournament-list")
end
end
end
As I run rspec there comes an error like this:
Failures:
1) Club privacy shouldn't be shown any tournament if the user is private
Failure/Error: visit "/user/#{user.id}/tournaments"
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_1:0x007ff0ea113108>
# ./spec/user_spec.rb:14:in `block (3 levels) in <top (required)>'
For capybara specs I use:
describe "Club", :type => :feature, :js => true do
include Capybara::DSL
it "..."
end
And instead User.all.each{|x| x.delete}, use DatabaseCleaner
You can easily add the Capybara::DSL to RSpec config spec_helper.rb
RSpec.configure do |config|
.....
.......
config.include(Capybara::DSL)
end