matt-burgess
matt-burgess

Reputation: 31

Rspec using test, Capybara using development database

I have an interesting problem. I'm using Rspec for test driven development and Capybara with Poltergeist for acceptance testing. Oh, and FactoryGirl, too. Rspec and FactoryGirl is operating against the test database, which is what I want. The problem is that acceptance tests are operating against the development database.

This causes simple tests like the following to fail:

  my_class = FactoryGirl.create(:my_class)
  visit my_classes_path
  expect(page).to have_content(my_class.title)

I've even checked screenshots along the way using:

page.save_screenshot("screenshot#{__FILE__}_#{__LINE__}.png")

SOLUTION So apparently Capybara was attempting to use the same URL and port that is initialized in my local environment when I kickoff "rails server". Specifying a different port in my Capybara configuration did the trick as seen below:

Capybara.configure do |c|
  c.run_server = true
  c.javascript_driver = :poltergeist
  c.default_driver = :poltergeist
  c.server_port = 7000
  c.app_host = "http://localhost:#{c.server_port}"
end

Upvotes: 3

Views: 762

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49870

For normal use you shouldn't have to lock to a specific port or set app_host. If app_host isn't set then Capybara defaults to http://#{Capybara.server_host}:#{Capybara.server_port} which by default is http://127.0.0.1:<the port capybara runs the server on> . If you need to use localhost instead of 127.0.0.1 (because of IPv6 or something) then just set

Capybara.server_host = 'localhost'

instead of app host and fixing the port. app_host is really for when you're trying to test an external site, or you need to access subdomains to test your app - and fixing the port is really intended for issues with firewalls, etc.

Upvotes: 2

Related Questions