Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test ActiveRecord callbacks with RSpec?

How to test the following example?

class Post < ActiveRecord::Base
  belongs_to :discussion, touch: true
end
like image 573
Hermine D. Avatar asked Apr 20 '10 08:04

Hermine D.


1 Answers

You can set a message expectation:

it "should touch the discussion" do
  post = Factory.build(:post)
  post.discussion.should_receive(:touch)
  post.save!
end

This examples uses Factory Girl, but you could use fixtures or mocks as well.

like image 122
zetetic Avatar answered Sep 28 '22 08:09

zetetic