如何使用RSpec Rails 4.0使用特定的URL测试json格式?

问题描述 投票:0回答:1

我需要测试“ /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
```
ruby-on-rails json testing rspec rspec-rails
1个回答
0
投票

在Rails 4中,您在传递参数时未定义params键:

get :show, format: :json, search: c

从Rails 5开始,使用params键传递参数:

get :show, format: :json, params: { search: c }
© www.soinside.com 2019 - 2024. All rights reserved.