Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Rails and RSpec equality tests have a variety of choices.</p> <p>Rails 3.2 ActiveRecord::Base uses the <code>==</code> equality matcher. </p> <p>It returns true two different ways:</p> <ul> <li>If <code>self</code> is the same exact object as the comparison object</li> <li>If <code>self</code> is the same type as the comparison object and has the same ID</li> </ul> <p>Note that ActiveRecord::Base has the <code>==</code> method which is aliased as <code>eql?</code>. This is different than typical Ruby objects, which define <code>==</code> and <code>eql?</code> differently.</p> <p>RSpec 2.0 has these equality matchers in rspec-expectations:</p> <pre><code>a.should equal(b) # passes if a.equal?(b) a.should eql(b) # passes if a.eql?(b) a.should == b # passes if a == b </code></pre> <p>RSpec also has two equality matchers intended to have more of a DSL feel to them:</p> <pre><code>a.should be(b) # passes if a.equal?(b) a.should eq(b) # passes if a == b </code></pre> <p>In your example you're creating a record then finding it.</p> <p>So you have two choices for testing <code>#find_by_name</code>:</p> <ul> <li><p>To test if it retrieves the exact same object <em>OR</em> an equivalent Person record with the same ID, then use <code>should ==</code> or its equivalent <code>a.should eql</code> or its DSL version <code>should eq</code></p></li> <li><p>To test if it uses the exact same object <em>NOT</em> an equivalent Person record with the same ID, then use <code>should equal</code> or its DSL version <code>should be</code></p></li> </ul>
 

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