我需要测试“ /users.json?search=“ +” [params]“这样的网址:
it "renders a successful response" do
get :show, params: "/users.json?search=c"
expect(response).to have_http_status(200)
expect(response).to be_successful
end
我的控制器喜欢
def index
@users = User.search(params[:search]).limit(10)
respond_to do |f|
f.json { render json: @users }
end
end
运行测试时出现此错误:
Failure/Error: before { get :show, params: "/users.json?search=unk" }
NoMethodError:
undefined method `symbolize_keys' for "/users.json?search=unk":String
```
在Rails 4中,您在传递参数时未定义params
键:
get :show, format: :json, search: c
从Rails 5开始,使用params
键传递参数:
get :show, format: :json, params: { search: c }