0

My test code in the spec/features/posts_spec.rb looks like this

require 'spec_helper'
feature 'Posts' do
  scenario 'Editing of Micropost', js: true do
     visit '/signin'
     fill_in 'Email', with: 'user@example.com
...

The code works fine with js: true. However if I take out js: true, the test fails at the fill_in 'Email' and if I use save_and_open_page immediately prior to this line I see

Not Found: /signin

My understanding is that I should not have to put the js: true unless I need to test a javascript function and the default rack_test driver should work. What is going wrong? My spec_helper file is as follows

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'capybara'
require 'capybara/rails'
require 'capybara/rspec'
require 'capybara-screenshot'
require 'capybara-screenshot/rspec'
require 'capybara/poltergeist'
require 'pp'
require Rails.root.join('app/services/dbg').to_s
require 'database_cleaner_support'
require 'shoulda_matchers_support'
require 'chris_matchers_support'
require 'chris_helpers_support'
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
Capybara.default_host = 'www.example.com'
RSpec.configure do |config|
  Capybara.default_driver = :rack_test
  Capybara.javascript_driver = :poltergeist
  Capybara::Webkit.configure(&:block_unknown_urls)
  Capybara::Screenshot.prune_strategy = { keep: 20 }
  Capybara::Screenshot.append_timestamp = false
  config.include Capybara::UserAgent::DSL
  config.include Rails.application.routes.url_helpers
  config.include ApplicationHelper
  config.include AccountsHelper
  config.mock_with :rspec
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = false
  config.filter_run focus: true
  config.run_all_when_everything_filtered = true
  ActiveRecord::Migration.maintain_test_schema! # dont need db:test:prepare
end
RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end
  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
  end
  # config.before(:each, :js => true) do
  #     DatabaseCleaner.strategy = :truncation
  # end
  config.before(:each) do |example|
    if example.example.metadata[:js]
      DatabaseCleaner.strategy = :truncation
    else
      DatabaseCleaner.strategy = :transaction
    end
    DatabaseCleaner.start
  end
  config.after(:each) do
    DatabaseCleaner.clean
  end
end
Obromios
  • 15,408
  • 15
  • 72
  • 127
  • do you use `database_cleaner` ? can you paste content of its config – itsnikolay Jul 20 '16 at 11:43
  • Have you tried doing the same thing manually with JavaScript disabled? – Mihai Dinculescu Jul 20 '16 at 11:54
  • How do you disable Javascript in rspec? – Obromios Jul 20 '16 at 11:57
  • I have added in the config for database_cleaner. – Obromios Jul 20 '16 at 11:58
  • In your routes.rb does your `/signin` route have any sort of dependency on the domain/subdomain name? – Thomas Walpole Jul 20 '16 at 17:31
  • No, the route definition is ```get '/signin', to: 'sessions#new'```, there is no use of subdomain and all the routes are relative. I suspect that somehow ```rack_test``` is not loading, any thoughts on how to test for this? – Obromios Jul 20 '16 at 20:36
  • My latest thinking is that this is because I am using webrat for my requests specs. I tried taking out webrat from the gemfile. This certainly stopped my request specs from working but did not fix the problem discussed here. I have searched all my files for any mention of webrat or Webrat and cannot find any references. – Obromios Jul 22 '16 at 07:03
  • No clue what's going on, I would investigate by using byebug and stepping into the visit call to see what's happening – Thomas Walpole Jul 22 '16 at 08:12

1 Answers1

0

The solution was to delete the line

Capybara.default_host = 'www.example.com'

I am not sure why this was causing the problem.

Obromios
  • 15,408
  • 15
  • 72
  • 127