Search code examples
ruby-on-rails-5rspec-railsrails-api

Rails API post in request spec should pass params but getting empty


To start, in my request spec sites.spec.rb I have this test:

describe "POST /v1/sites" do

    let(:valid_attributes) { { url: "www.example.com", site_code: "123456" } }

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

      it 'creates a site' do
        expect(json['url']).to eq("www.example.com")
        expect(json['site_code']).to eq("123456")
      end

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

I then get a failing test for "creates a site"...

1) Sites POST /v1/sites when the request is valid creates a site
     Failure/Error: expect(json['url']).to eq("www.example.com")

       expected: "www.example.com"
            got: ["can't be blank"]

       (compared using ==)
     # ./spec/requests/sites_spec.rb:61:in `block (4 levels) in <top (required)>'
     # ./spec/rails_helper.rb:84:in `block (3 levels) in <top (required)>'
     # ./spec/rails_helper.rb:83:in `block (2 levels) in <top (required)>'

Now this technically makes sense because my Site model has validates :url, :site_code, presence: true. So the test is failing because the post is not passing the params correctly.

Lastly, here is the controller:

module Api::V1
  class SitesController < BaseApiController
    before_action :set_site, only: [:show, :update, :destroy]

    # GET /sites
    def index
      @sites = Site.all

      render json: @sites
    end

    # GET /sites/1
    def show
      render json: @site
    end

    # POST /sites
    def create
      @site = Site.new(site_params)

      if @site.save
        render json: @site, status: :created, location: @site
      else
        render json: @site.errors, status: :unprocessable_entity
      end
    end

    # PATCH/PUT /sites/1
    def update
      if @site.update(site_params)
        render json: @site
      else
        render json: @site.errors, status: :unprocessable_entity
      end
    end

    # DELETE /sites/1
    def destroy
      @site.destroy
    end

    private
      # Use callbacks to share common setup or constraints between actions.
      def set_site
        @site = Site.find(params[:id])
      end

      # Only allow a trusted parameter "white list" through.
      def site_params
        # params.require(:data).require(:attributes).permit(:url, :side_code, :user_id)
        # params.require(:site).permit(:url, :side_code, :user_id)
        params.fetch(:site, {}).permit(:url, :side_code)
      end
  end
end

I am speculating that the way I am passing parameters to the post for Rails API is perhaps not formatted or correctly or something else entirely. I did play with the params in the test block trying data: { attributes: valid_attributes } with no luck.

Any thoughts or suggestions are greatly appreciated!


Solution

  • This problem was indeed due to format of the parameters I was passing to POST request in the test block. I tested the POST via the command line and watched the rails server to see how the params were coming through. They looked like this:

    Parameters: {"site_code"=>"123456", "url"=>"www.updated.com", "subdomain"=>"api", "id"=>"2", "site"=>{"site_code"=>"123456", "url"=>"www.updated.com"}}
    

    Then in my sites_spec.rb, I copied this format for the valid params of the post request:

    let(:valid_attributes) { { "site"=>{"url"=>"www.example.com", "user_id"=>user_id, "site_code"=>"123456"} } }
    

    This works. The JSON format of the params needed to be formatted in the test block the same way they would be if it was real JSON request.