0

使用以下 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 而不是按名称排序的。有谁知道为什么会发生这种情况?

谢谢!

4

1 回答 1

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]
于 2012-04-21T21:03:15.957 回答