Search code examples
ruby-on-railsrspecintegration-testingacceptance-testing

cabybara-webkit + rspec: Records are not available


I'm new to the integration testing, although I've done a lot of unit tests for my models and controllers. But now I want to test the whole stack. (I don't want to use Cucumber, since there is no customer and I can read code)

Here my (simplified) spec

describe ArticlesController, "#show" do
  before do
    @article = Factory :article, :title => "Lorem ipsum"
  end

  it "should show the article page" do
    visit article_path(:id => @article)
    page.should have_content("Lorem ipsum")
  end
end

The spec passes, but once I add :js => true to it "should show the article page", :js => true do, an ActiveRecord::RecordNotFound gets thrown. If I disable use_transactional_fixtures in my config, it works again, but that breaks alot of other tests. Is there another solution or can I disable the transactional_fixtures just for my integration tests?

Thanks for reading! :)


Solution

  • You need to set use_transactional_fixtures = false for integration tests. As you found, this causes problems for other tests that assume your tables are empty to start with.

    You might try the database_cleaner gem. A typical configuration in spec_helper would look like so:

    RSpec.configure do |c|
      c.before(:suite) do
        DatabaseCleaner.start
      end
    
      c.after(:suite) do
        DatabaseCleaner.clean
      end
    end