1

I have a simple test case that looks like this:

describe 'Quiz packs page' do
  before :each do
    FactoryGirl.create(:quiz_pack, :first_in_first_pack).should be_valid
  end
end

it "should have an index page", :js => true do
  visit quiz_packs_path
end

Why, when I visit the quiz_packs_path using selenium, do my traits from FactoryGirl not show up as an entry on the index page?

Honestly, I have read so much about FactoryGirl that my brain hurts, but I still don't quite understand how the creation occurs.

Here is my quiz pack if it helps. Please note I'm using a FactoryGirl trait because I have a bunch of these things. If this is an issue, please let me know.

FactoryGirl.define do
  factory :quiz_pack do
    trait :first_in_first_pack do |f|
      f.pack 1
      f.number 1
      f.image "first_image
    end
  end
end
dblarons
  • 123
  • 2
  • 11
  • 2
    are you using database cleaner and non transactional fixtures? or are you sharing your DB connection. What happens without the above is that the factory creates the object inside a transaction and when selenium connects it creates a new connection and that transaction doesn't exist on that connection so it doesn't exist. see http://stackoverflow.com/questions/8774227/why-not-use-shared-activerecord-connections-for-rspec-selenium – Doon Apr 10 '13 at 23:18
  • I'm going to Google all of those words and get back to you when I know what I'm talking about. – dblarons Apr 10 '13 at 23:20
  • 1
    That's almost definitely the problem. One "solution" is to separate your integration tests from the rest of your unit tests, and only use database cleaner for those. Generally speaking, you won't notice much difference between database cleaner and transactional fixtures, but it doesn't feel right to me to abandon transactions if there are alternatives. Especially if you have any tests with things like `after_commit` hooks. – Jim Stewart Apr 10 '13 at 23:21
  • Okay, so aside from copy-pasting the code for spec_helper.rb that Doon included in the link in his comment, what else should I do? (Oh, and thanks Doon, that fixed it!) Should I put all of my Selenium tests inside an integration folder and run them separately? Then what database cleaner should I use (or do you recommend I use)? Also, can I get a quick layman's explanation of the difference between database cleaners and transactional fixtures? – dblarons Apr 10 '13 at 23:32

1 Answers1

5

This is a good question. I don't have this problem because I always setup rspec properly in the beginning. But the issue is still worth to address.

And here is the solution

This time the first spec fails as the content “paint fence” is missing from the page. The database record that is created isn’t available to the Selenium tests and this is because our specs are using database transactions which aren’t compatible with Selenium. To fix this we can set the config.use_transactional_fixtures setting in the spec_helper file to false. http://asciicasts.com/episodes/257-request-specs-and-capybara

So, just set this in spec_helper.rb you should be good to go.

config.use_transactional_fixtures = false

I've checked it on one of my projects and it works well.

Additionally, database cleaner is always preferred in any circumstance.

And here is a detailed discussion if you are interested to read, though not necessary:https://groups.google.com/forum/#!msg/ruby-capybara/JI6JrirL9gM/R6YiXj4gi_UJ

Billy Chan
  • 24,625
  • 4
  • 52
  • 68
  • Thanks for the info. How might I go about setting up rspec "properly" next time? Simply adding that line, or something more such as integration testing? – dblarons Apr 11 '13 at 01:27
  • I would also like to point out for any future readers that the info at link was originally what fixed my problem: http://stackoverflow.com/questions/8774227/why-not-use-shared-activerecord-connections-for-rspec-selenium – dblarons Apr 11 '13 at 01:28
  • 1
    @midnightdev, I just checked my notes. This setting is nothing to do with rspec setting, but a setting for DatabaseCleaner. You can check more here: http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium – Billy Chan Apr 11 '13 at 01:45
  • This is great! I wish there were more great explanations like that! Thanks and I'll read up until I understand it well. – dblarons Apr 12 '13 at 02:14