我使用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测试for create?
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