[英]how do I use curl to test my Rails controller create action.. or can I use rspec?
[英]How would I turn this curl request into an RSpec test for this controller action
我使用 curl 來測試 a 以查看此控制器是否會接收傳入請求,但現在我想進行 RSPEC 測試,我將如何轉動此 curl 行: curl -v -H "Accept: application/json" -H "Origin: requesting.url.site" -H "Content-Type: application/json" -X POST -d '{"name":"foobar"}' http://arbitrary.site
進入 RSPEC Post 測試以進行創建?
class API::EventsController < ApplicationController
skip_before_action :verify_authenticity_token #for this example skip the authenticity_token
before_action :set_access_control_headers
def preflight
head 200
end
def set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
headers['Access-Control-Allow-Headers'] = 'Content-Type'
end
def create
registered_application = RegisteredApplication.find_by(url: request.env['HTTP_ORIGIN'])
if registered_application == nil
render json: {errors: "Unregistered application"}, status: :unprocessable_entity
else
@event = registered_application.events.new(event_params)
if @event.save
render json: @event, status: :created
else
render json: @event.errors, status: :unprocessable_entity
end
end
end
private
def event_params
params.require(:event).permit(:name)
end
end
您應該將標題附加到通常的 rspec 控制器發布操作。
已在此處回答將標頭附加到 Rspec 控制器測試
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.