【发布时间】:2014-03-19 08:54:25
【问题描述】:
我需要在我的控制器中测试自动完成操作。我的测试只需要期望返回计数,但我不知道该怎么做。
这是方法本身:
def autocomplete
respond_to do |format|
format.json do
model_suggestions = Vehicle.select([:model, :make]).where('make IS NOT NULL AND model IS NOT NULL').reorder('').order("make ASC, model ASC")
if params.has_key? :model
model_suggestions = model_suggestions.group(:model)
else
model_suggestions = model_suggestions.group(:make)
end
unless params[:model].blank?
model_suggestions = model_suggestions.where('model LIKE ?', "%#{params[:model]}%")
end
unless params[:make].blank?
model_suggestions = model_suggestions.where('make LIKE ?', "%#{params[:make]}%")
end
model_list = {model: [], make: []}
model_suggestions.each do |car|
model_list[:make] << car[:make].strip
model_list[:model] << car[:model].strip
end
model_list[:make].uniq!
model_list[:model].uniq!
render json: model_list
end
end
end
路线如下:
get :autocomplete, constraints: {format: 'json'}
这是我需要的 rspec 测试。我什么都想不出来。我对 Rails 和 TDD 很陌生。所以请不要对我这么苛刻
describe "GET #autocomplete" do
before do
FactoryGirl.create(:vehicle, make: "AUDI", model: "A4")
FactoryGirl.create(:vehicle, make: "BMW", model: "M5")
FactoryGirl.create(:vehicle, make: "BMW", model: "M3")
end
it "responds to with 2 makers" do
get :autocomplete, :format => :json #giving param make: "BMW"
# expect count 2
end
end
我怎么能做到这一点???非常感谢:)
【问题讨论】:
标签: ruby-on-rails ruby json rspec