user938363
user938363

Reputation: 10350

record generated by FactoryGirl disappears in rspec

We are running a spec (3.4.4) with Rails 4.2.0. Here is the spec:

  it "returns http success" do
    proj = FactoryGirl.create(:ext_construction_projectx_project, :name => 'namenewnew')
    get 'new' , {:project_id => proj.id}
    expect(response).to be_success
  end

In debug, proj.id is 3 and all its values are correct. ExtConstructionProjectx::Project.find(3) returns nil and ExtConstructionProjectx::Project.count returns 0. The project record just disappears.

Is it possible that the rspec did not see the project record created on its db connection? How to fix this problem?

Update:

Here is the gemspec

  s.add_dependency "rails", ">=4.2.0"
  s.add_dependency "jquery-rails"
  s.add_dependency "simple_form"
  s.add_dependency "will_paginate"
  s.add_dependency "database_cleaner"
  s.add_dependency "execjs"
  s.add_dependency "sass-rails", '~>5.0.1'
  s.add_dependency "coffee-rails", '~>4.1.0'   
  s.add_dependency "uglifier"

  s.add_development_dependency "sqlite3"
  s.add_development_dependency "rspec-rails", ">= 3.2.0"
  s.add_development_dependency "factory_girl_rails", '~>4.5'
  s.add_development_dependency 'capybara'
  s.add_development_dependency 'launchy'

Also in rails_spec, there is:

config.use_transactional_fixtures = true

Upvotes: 2

Views: 190

Answers (1)

Scott Swezey
Scott Swezey

Reputation: 2127

This is expected behavior. If database records persisted between test calls, your testing wouldn't be isolated or properly test each case.

If you don't want the current behavior:

  • Consider removing (or reconfiguring) the database_cleaner gem.
  • Set config.use_transactional_fixtures = false in your rspec configuration. Or review the rspec docs for another strategy.

The "better" solution:

  • Rewrite your tests such that they do not depend on state from earlier test runs.
  • Consider using before(:each) and before(:all) macros in your rspec tests.

Upvotes: 2

Related Questions