Note that there are some explanatory texts on larger screens.

plurals
  1. PORailstutorial.org Validating unique email
    text
    copied!<p>In section 6.2.4 of <i>Ruby on Rails 3 Tutorial</i>, Michael Hartl describes a caveat about checking uniqueness for email addresses: If two identical requests come close in time, request A can pass validation, then B pass validation, then A get saved, then B get saved, and you get two records with the same value. Each was valid at the time it was checked.</p> <p>My question is <b>not</b> about the solution (put a unique constraint on the database so B's save won't work). It's about writing a test to prove the solution works. I tried writing my own, but whatever I came up with only turned out to mimic the regular, simple uniqueness tests.</p> <p>Being completely new to rspec, my naive approach was to just write the scenario:</p> <pre><code>it 'should reject duplicate email addresses with caveat' do A = User.new( @attr ) A.should be_valid # always valid B = User.new( @attr ) B.should be_valid # always valid, as expected A.save.should == true # save always works fine B.save.should == false # this is the problem case # B.should_not be_valid # ...same results as "save.should" end </code></pre> <p>but this test passes/fails in exactly the same cases as the regular uniqueness test; the <code>B.save.should == false</code> passes when my code is written so that the regular uniqueness test passes and fails when the regular test fails.</p> <p>So my question is "how can I write an rspec test that will verify I'm solving this problem?" If the answer turns out to be "it's complicated", is there a different Rails testing framework I should look at?</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