Search code examples
ruby-on-rails-3rspecarel

How would one spec ActiveRecord::Base.find(something).method with should_receive?


Let's say we have the following code:

class Post < ActiveRecord::Base
  def self.fix_published_times
    where(:published => true, :published_at => nil).limit(5).each do |post
      post.fix_published_at!
    end
  end
end

How should I spec Post#fix_published__at! with should_receive?


Solution

  • Something along this lines of this should work:

      mock_posts = [mock("post 1"), mock("post 2")]
      Post.should_receive(:where).with(:published => true, :published_at => nil).and_return(mock_posts)
      mock_posts.each do |mp|
        mp.should_receive(:fix_published_at!).and_return(true)
      end
      Post.fix_published_times