Rails test db doesn't persist record changes

Posted by nathan.f77 on Stack Overflow See other posts from Stack Overflow or by nathan.f77
Published on 2010-03-30T00:59:40Z Indexed on 2010/03/30 1:03 UTC
Read the original article Hit count: 603

I've been trying to solve a problem for a few weeks now. I am running rspec tests for my Rails app, and they are working fine except for one error that I can't seem get my head around.

  • I am using MySQL with the InnoDB engine.
  • I have set config.use_transactional_fixtures = true in spec_helper.rb
  • I load my test fixtures manually with the command rake spec:db:fixtures:load.
  • The rspec test is being written for a BackgrounDRb worker, and it is testing that a record can have its state updated (through the state_machine gem).

Here is my problem:

I have a model called Listings. The rspec test calls the update_sold_items method within a file called listing_worker.rb. This method calls listing.sell for a particular record, which sets the listing record's 'state' column to 'sold'. So far, this is all working fine, but when the update_sold_items method finishes, my rspec test fails here:

listing = Listing.find_by_listing_id(listing_id)
listing.state.should == "sold"

expected: "sold",
     got: "current" (using ==)

I've been trying to track down why the state change is not persisting, but am pretty much lost. Here is the result of some debugging code that I placed in the update_sold_items method during the test:

pp listing.state  # => "current"

listing.sell!
listing.save!

pp listing.state  # => "sold"

listing.reload

pp listing.state  # => "current"

I cannot understand why it saves perfectly fine, but then reverts back to the original record whenever I call reload, or Listing.find etc.

Thanks for reading this, and please ask any questions if I haven't given enough information.

Thanks for your help, Nathan B

P.S. I don't have a problem creating new records for other classes, and testing those records. It only seems to be a problem when I am updating records that already exist in the database.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about persistent