如何使用should_receive规范ActiveRecord :: Base.find(something).method?

时间:2011-03-23 11:40:33

标签: ruby-on-rails-3 rspec arel

假设我们有以下代码:

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

我应该如何使用should_receive规范 Post#fix_published__at!

1 个答案:

答案 0 :(得分:1)

这方面的某些方面应该有效:

  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