Rspec和_return按ID排序?

时间:2012-04-21 20:36:58

标签: ruby-on-rails testing rspec

使用以下rspec测试:

@search_text = "California"
@search_condition = "location"
Video.stub_chain(:where,:order).and_return([@vid1,@vid2,@vid3])
Video.should_receive(:where).with("lower(#{@search_condition}) LIKE ?", "%#{@search_text}%".downcase)

return_val = Video.search(@search_text, @search_condition)
return_val.should == [@vid1,@vid2,@vid3]

测试以下范围:

  scope :search, (lambda do |search_text, search_condition|
                where("lower(#{search_condition}) LIKE ?", "%#{search_text}%".downcase).order("name")
              end)

我为return_value获取的值不会使我返回的值为stub_chain。特别是,返回的数组中的项是相同的,但它们的顺序似乎按id排序,而不是按名称排序。有谁知道为什么会这样?

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

unordered_videos = double "Video"
Video.should_receive(:where).with("lower(#{@search_condition}) LIKE ?", "%#{@search_text}%".downcase).and_return unordered_videos
unordered_videos.should_receive(:order).and_return [@vid1,@vid2,@vid3]