Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>referring to @danivovich example</p> <pre><code>class Part &lt; ActiveRecord::Base before_validation :strip_whitespace protected def strip_whitespace self.title = self.title.strip end end </code></pre> <p>the proper way to write the spec is to separately write spec on <strong>strip_whitespace</strong> method and then just check if model class have callback set on it, like this:.</p> <pre><code>describe Part do let(:record){ described_class.new } it{ described_class._validation_callbacks.select{|cb| cb.kind.eql?(:before)}.collect(&amp;:filter).should include(:strip_whitespace) } #private methods describe :strip_whitespace do subject{ record.send(:strip_whitespace)} # I'm using send() because calling private method before{ record.stub(:title).and_return(' foo ') it "should strip white spaces" do subject.should eq 'foo' # or even shorter should eq 'foo' end end end </code></pre> <p>if you need to skip callback behavior in some scenarios use</p> <pre><code>before{ Part.skip_callback(:validation, :before, :strip_whitespace)} before{ Part.set_callback( :validation, :before, :strip_whitespace)} </code></pre> <p><strong>Update 20.01.2013</strong></p> <p>BTW I wrote a gem with RSpec matchers to test this <a href="https://github.com/equivalent/shoulda-matchers-callbacks" rel="nofollow">https://github.com/equivalent/shoulda-matchers-callbacks</a> </p> <p>In general I don't recommend callback at all. They are fine for example in this question, however once you do more complicated stuff like:</p> <p>After create-></p> <ul> <li>link account to user</li> <li>create notification</li> <li>send email to Admin</li> </ul> <p>...then you should create custom service object to deal with this and test them separately.</p> <ul> <li><a href="http://railscasts.com/episodes/398-service-objects" rel="nofollow">http://railscasts.com/episodes/398-service-objects</a></li> <li><a href="http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/" rel="nofollow">http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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