geography_guy
geography_guy

Reputation: 331

When running rake:cucumber ActiveRecord models cannot access same database as app

I am using Cucumber with Watir web browser to automate tests. I am using Watir to fill out a web form which saves a model to my database. However, when I try to confirm that the record exists using ActiveRecord, my search does not return any results. I suppose I could use Watir to automate every step of my tests, but in the future my test suite will start to run VERY slowly.

My feature:

Feature: Creating a campaign

As someone with too much time on my hands
I want to create a campaign
So that I can monitor my roleplaying campaigns

Scenario: Adding a Campaign
    Given I am at the site
    When I create a campaign "The Hills" with a setting "Cali"
    Then the campaign should persist in the system

My step definitions:

Given /^I create a campaign "([^"]*)" with a setting "([^"]*)"$/ do |name, setting|
  @browser.text_field(:id => "campaign_campaign_name").set name
  @browser.text_field(:id => "campaign_setting").set setting
  @browser.button(:value => "Create Campaign").click
  @campaign_name = name
  @setting = setting
end

Then /^the campaign should persist in the system$/ do
  @browser.text.should =~ /#{@campaign_name}/
  @browser.text.should =~ /#{@setting}/
  @database_campaign = Campaign.where(:campaign_name => @campaign_name)[0]
  @database_campaign.campaign_name.should == @campaign_name
end

NOTE: I tried explicitly establishing a connection to my test database, but that didn't work either. I wrote a ruby file in the support directory containing this:

def establish_connection_to_test_database
  begin
    ActiveRecord::Base.establish_connection :test
  rescue
    raise "You have not established a connection to the test database"
  end
end

establish_connection_to_test_database

Upvotes: 1

Views: 712

Answers (1)

Parker Selbert
Parker Selbert

Reputation: 1546

ActiveRecord uses connection pools to allow different threads to access the same database. Connecting to the same database is probably not the problem here. It is most likely that your verification step is running too fast and the web transaction hasn't completed yet, causing a race condition. You can test this by putting a sleep call in your verification step:

Then /^the campaign should persist in the system$/ do
  sleep 1.0

  @browser.text.should =~ /#{@campaign_name}/
  @browser.text.should =~ /#{@setting}/
  @database_campaign = Campaign.where(:campaign_name => @campaign_name)[0]
  @database_campaign.campaign_name.should == @campaign_name
end

That said, you are testing two separate things in the same step: Text on the page, and that the data was persisted to the database (which is also done redundantly).

I recommend avoiding the database check unless you feel it is absolutely necessary, which should also fix the race condition.

Then /^the campaign should persist in the system$/ do
  @browser.text.should include(@campaign_name)
  @browser.text.should include(@setting)
end

Upvotes: 1

Related Questions