Note that there are some explanatory texts on larger screens.

plurals
  1. PORails test db doesn't persist record changes
    text
    copied!<p>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. </p> <ul> <li>I am using MySQL with the InnoDB engine.</li> <li>I have set <code>config.use_transactional_fixtures = true</code> in spec_helper.rb</li> <li>I load my test fixtures manually with the command <code>rake spec:db:fixtures:load</code>.</li> <li>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).</li> </ul> <p>Here is my problem:</p> <p>I have a model called <code>Listings</code>. The rspec test calls the <code>update_sold_items</code> method within a file called <code>listing_worker.rb</code>. This method calls <code>listing.sell</code> for a particular record, which sets the listing record's 'state' column to 'sold'. So far, this is all working fine, but when the <code>update_sold_items</code> method finishes, my rspec test fails here:</p> <pre><code>listing = Listing.find_by_listing_id(listing_id) listing.state.should == "sold" expected: "sold", got: "current" (using ==) </code></pre> <p>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 <code>update_sold_items</code> method during the test:</p> <pre><code>pp listing.state # =&gt; "current" listing.sell! listing.save! pp listing.state # =&gt; "sold" listing.reload pp listing.state # =&gt; "current" </code></pre> <p>I cannot understand why it saves perfectly fine, but then reverts back to the original record whenever I call <code>reload</code>, or <code>Listing.find</code> etc.</p> <p>Thanks for reading this, and please ask any questions if I haven't given enough information.</p> <p>Thanks for your help, Nathan B</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload