0

假设我们有以下代码:

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

我应该如何 Post#fix_published__at! 指定 should_receive?

4

1 回答 1

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
于 2011-03-23T11:56:37.610 回答