slindsey3000
slindsey3000

Reputation: 4291

Database not getting set up with :js => true

With the code below my browser fires up for the test. I can see the rendered page. Items are missing on the page that are supposed to be in the database. Why isn't my database getting populated?

When I get rid of ":js =>true" the database is set up.. but the test fails because I need javascript to click on a table row.

require 'spec_helper'

feature "[user can add analysis to a line]", :js => true do

  context "[User signed in]" do

    scenario "[user can visit home page click on a line and add analysis]"  do

      puts "** Add analysis feature spec not working **"

      jim = Fabricate(:user)
      sign_in(jim)
      nfl = Fabricate(:league, name: "NFLXX")
      nba = Fabricate(:league, name: "NBA")
      nhl = Fabricate(:league, name: "NHL")
      mlb = Fabricate(:league, name: "MLB")
      nfl_event1 = Fabricate(:event, league: nfl, home_team: "Eagles")
      nfl_event2 = Fabricate(:event, league: nfl, home_team: "Jets")
      nba_event = Fabricate(:event, league: nba, home_team: "Sixers")
      nhl_event = Fabricate(:event, league: nhl, home_team: "Flyers")
      mlb_event = Fabricate(:event, league: mlb, home_team: "Phillies")

      visit(posts_path)    
      find('.league-text').click_link("All")
      page.all('.vegas-line')[0].click
      click_link("ADD Analysis")
      fill_in('post_title', :with => "This is my analysis")
      fill_in('post_content', :with => "This is a long analysis for this game. If you like it you should say something to me")
      click_button('post')
      page.should have_content "Post submitted sucessfully"

    end

  end
end

Upvotes: 0

Views: 88

Answers (1)

Steve
Steve

Reputation: 15736

I think the problem you have is that when your tests are run with JavaScript your test code and application server code are running in separate threads and therefore don't share the same database connection.

If you have rspec-rails running with the default configuration you will be using database transactions for cleanup (the test is wrapped in a transaction that is rolled back at the end). This doesn't work across multiple threads or processes. See https://github.com/jnicklas/capybara#transactions-and-database-setup

The most common way to solve this problem is to use the database cleaner gem - https://github.com/bmabey/database_cleaner

Upvotes: 1

Related Questions