Reputation: 407
I'm putting together feature specs for an existing Rails app using Capybara and RSpec. By default, Capybara uses rack_test
as the driver, but I want to use capybara-webkit.
I've put together a simple test to log in to my site. Using rack_test
I'm able to log in, and my log/test.log
file shows the following HTTP requests:
Completed 200 OK in 201ms (Views: 201.3ms | ActiveRecord: 0.0ms)
Started POST "/users/sign_in" for 127.0.0.1 at 2015-12-01 10:20:33 -0600
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign In"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' ORDER BY "users"."id" ASC LIMIT 1
# There are a few more SQL queries here that I've removed
Redirected to http://www.example.com/
Completed 302 Found in 36ms (ActiveRecord: 2.3ms)
Started GET "/" for 127.0.0.1 at 2015-12-01 10:20:33 -0600
Processing by InitiativesController#blank as HTML
I'm having issues with running this test when I switch to webkit (by setting Capybara.default_driver :webkit
in spec_helper.rb
). For some reason I am seeing 401 errors in the log:
Completed 200 OK in 17ms (Views: 16.7ms | ActiveRecord: 0.0ms)
Started POST "/users/sign_in" for 127.0.0.1 at 2015-12-01 10:21:31 -0600
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign In"}
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' ORDER BY "users"."id" ASC LIMIT 1
# no other queries
Completed 401 Unauthorized in 5ms
Processing by Devise::SessionsController#new as HTML
So the log in request is not being accepted. Is there some sort of Capybara configuration that I need to change in order to get this log in request to work? I can't figure out what is causing this.
My spec_helper.rb
looks like this:
require 'simplecov'
require 'webmock/rspec'
require 'capybara/rspec'
require 'fakeredis/rspec'
require 'site_prism'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
Capybara.javascript_driver = :poltergeist
Capybara.default_driver = :webkit
Capybara.default_host = 'http://127.0.0.1:60400'
Capybara.server_port = 60400
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
Capybara::Webkit.configure do |config|
config.block_unknown_urls
end
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.infer_base_class_for_anonymous_controllers = false
config.include Devise::TestHelpers, :type => [:controller, :acceptance]
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do |example|
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.order = "random"
config.before(:all) do
FactoryGirl.reload
end
config.before :each do
Timecop.return
end
config.include Helpers
config.include Helpers::MassAdsCreation
end
WebMock.disable_net_connect!(allow_localhost: true)
Upvotes: 0
Views: 551
Reputation:
I think you may need to use the Warden::Test::Helpers
instead as described here
Note that they have a specific section dealing with capybara-webkit
as well.
Upvotes: 1
Reputation: 49890
The 401 probably means the password doesn't match the email you're using to login. Since its working for you with rack_test but not capybara-webkit it's most likely you are still using transaction based testing.
When using rack_test the application is run in the same thread as the tests which means they are both using the same database connection. Because of that you can used transaction based testing and never have to pay the performance penalty of actually writing anything to the database. However when using any "real browser" driver Capybara starts your app in it's own thread which means the tests and the app are using separate database connections and you therefore have to actually commit to the database. This is generally done by using database_cleaner in truncation mode. See https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example
Upvotes: 0