rails的新手。学习如何在Rails中使用以下方法创建API http:/apionrails.icalialabs.combookchapter_three。 有下面这个摘录,它模拟了一个 "get "请求,但我想知道如何才能查看该方法的参数是什么,以及它存在于哪个类中。在这一点上,它看起来像魔术,我不知道它是否是一个rspec ruby rails或一个gem,我已经厌倦了通过文档搜索,我真的很想通过代码找到它(一些运行时间的方式也许)。
require 'spec_helper'
describe Api::V1::UsersController do
before(:each) do
request.headers['Accept'] = "application/vnd.marketplace.v1"
end
describe "GET #show" do
before(:each) do
@user = FactoryBot.create :user
get :show, params: { id: @user.id }, format: :json
end
it "returns the information about a reporter on a hash" do
user_response = JSON.parse(response.body, symbolize_names: true)
expect(user_response[:email]).to eql @user.email
end
it { should respond_with 200 }
end
end
如果你不知道 rspec / ruby / rails
那就很难理解了,但让我试试。
describe Api::V1::UsersController
这说明该规范是为 Api::V1::UsersController
你会发现它在 app/controllers/api/v1/users_controller.rb
get :show, params: { id: @user.id }, format: :json
这一行告诉大家,呼叫 show
上述控制器的动作,参数为 { id: @user.id }
和请求类型 json
如果你想检查在 show
上述控制器的动作可以使用 byebug
公用事业检查 params
, request
, controller_name
, action_name
等
检查 https:/github.comdeivid-rodriguezbyebug。 了解如何使用byebug调试应用程序。