9
votes

I'm using RSpec, Spork, Capybara and Capybara Mechanic to write integration tests for a registration path that uses Facebook connect. It correctly navigates through registration, but the new users are created in my development database instead of the test database.

RSpec is setup to use the test environment and I've confirmed that any model methods I run in my spec all hit the test database, but all of the UI actions hit development.

Here's my test:

it "go through registration path" do
  print "RAILS_ENV: #{Rails.env}\n" # correctly prints 'test'
  print "1) #{User.count}\n" # correctly shows the user count in my test database (zero)

  Capybara.current_driver = :mechanize

  visit root_path
  click_link "register"

  # Fill in Facebook Connect form
  fill_in 'email', :with => "[email protected]"
  fill_in 'pass', :with => "password"
  click_button "Log In"

  print "2) #{User.count}\n" # still shows zero users in the test database

end

After that test I can look at my development database and the new user has been created there.

I've tried setting up database_cleaner as well and that hasn't helped.

Anyone know what I need to do so that capybara will hit the test database?

rails 3.1.3

rspec 2.7.0

spork 0.9.0

capybara 1.1.0

capybara-mechanize 0.3.0.rc3

1
Did you end up resolving this issue for yourself? I'm experiencing a similar issue in an app I'm working on. I'm trying to login with a record in test but it is saying the record doesn't exist. So I was thinking it was related to using the dev environment, but adding the record to dev is saying it doesn't exist either. So I'm not sure if Capybara is using a different database/environment entirely than rspecs test environment.JDutil
See my answer for the fix. How are you creating your users? I'm using FactoryGirl which inserts into the test database.Chris
I use FactoryGirl also. The records are being created within the test database as intended. When I change the driver to Selenium or Webkit and the browser needs to interact with the records they dont get returned though... I've tried setting the server settings like your answer also since I was thinking my POW configuration was causing it to run in dev mode in the browser, but that isn't the case either.JDutil
How are you testing that your records are actually in the right DB? Did you try printing ENV["RAILS_ENV"] and DB records to console from your test? Does your app interact with any third party sites that redirect back to your localhost? We use FB connect and it was redirecting back to localhost:3000 which had to be updated for my test since all the test data was at localhost:7000.The only other thing I can think of is emailing the Capybara Google group or writing an issue on their Github page.Chris
My issue was not setting transactional fixtures to false...JDutil

1 Answers

8
votes

The problem was that I had a separate development server running in parallel with the tests and the tests were defaulting to a the same address/port. I resolved this by adding the following to spec_helper.rb so Capybara would use a different port.

Capybara.run_server = true 
Capybara.server_port = 7000
Capybara.app_host = "http://localhost:#{Capybara.server_port}"