简体   繁体   English

RSpec发送原始JSON参数后请求

[英]RSpec sending raw JSON parameters post request

So I'm trying to test a post request in order to save some book details. 因此,为了节省一些图书详细信息,我正在尝试测试发布请求。 The response comes as raw JSON due to its being stringified in the client using formData. 响应作为原始JSON出现,因为它在客户端使用formData进行了字符串化处理。 So that I can format the response appropriately in the controller. 这样我就可以在控制器中正确设置响应的格式。 I cannot find any clear way to send raw JSON parameters as rails automatically coerce these parameters as a HASH. 我找不到任何清晰的方法来发送原始JSON参数,因为rails会自动将这些参数强制为HASH。 Any suggestion? 有什么建议吗? Rails 5.1.4 Rspec 3.7.2 Rails 5.1.4 Rspec 3.7.2

books_spec.rb books_spec.rb

# Test suite for POST /books
  describe 'POST /books' do
    # valid payload
    let(:valid_attributes) do
      # send stringify json payload
      { 
        "title": "Learn Elm", 
        "description": "Some good", 
        "price": 10.20, 
        "released_on": Time.now,
        "user_id": user.id,
        "image": "example.jpg"
      }.to_json
    end

    # no implicit conversion of ActiveSupport::HashWithIndifferentAccess into String

    context 'when the request is valid' do
      before { post '/books', params: valid_attributes, headers: headers }

      it 'creates a books' do
        expect(json['book']['title']).to eq('Learn Elm')
      end

      it 'returns status code 201' do
        expect(response).to have_http_status(201)
      end
    end

    context 'when the request is invalid' do
      let(:valid_attributes) { { title: nil, description: nil }.to_json }
      before { post '/books', params: valid_attributes, headers: headers }

      it 'returns status code 422' do
        expect(response).to have_http_status(422)
      end

      it 'returns a validation failure message' do
        expect(response.body)
          .to match(/Validation failed: Title can't be blank, Description can't be blank/)
      end
    end
  end

books_controller.rb books_controller.rb

# POST /books
def create
  @book = current_user.books.create!(book_params)
  render json: @book, status: :created
end

def book_params
  binding.pry # request.params[:book] = HASH
  parsed = JSON.parse(request.params[:book])
  params = ActionController::Parameters.new(parsed)
  params['image'] = request.params[:image]
  params.permit(
   :title, 
   :description, 
   :image, 
   :price, 
   :released_on, 
   :user
  )
end
 #you should try test the request first like this and if you see this helping you I will send you the 200 test

describe "#create" do
  context "when the request format is not json" do

    let(:error_message) do
      { "Invalid Request Format" => "Request format should be json" }
    end

    before do
      post :create, headers: { "CONTENT_TYPE": "XML" }
    end

    it "should return a status of 400" do
      expect(response).to have_http_status(400)
    end

    it "should return an invalid request format in the body" do
      expect(json(response.body)).to eql error_message
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM