请求规范中的Rails API帖子应该传递params但是变空

时间:2017-04-01 08:28:29

标签: ruby-on-rails-5 rspec-rails rails-api

首先,在我的请求规范sites.spec.rb中,我进行了此测试:

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

然后我对“创建网站”进行了失败的测试......

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)>'

现在这在技术上是有道理的,因为我的Site模型有验证:url,:site_code,presence:true。所以测试失败了,因为帖子没有正确地传递参数。

最后,这是控制器:

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

我猜测我将参数传递给Rails API的帖子的方式可能没有格式化或正确或完全不同。我尝试使用测试块中的params来尝试数据:{attributes:valid_attributes}但没有运气。

非常感谢任何想法或建议!

1 个答案:

答案 0 :(得分:0)

这个问题确实是由于我在测试块中传递给POST请求的参数格式。我通过命令行测试了POST,并观察了rails服务器,看看params是如何通过的。它们看起来像这样:

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

然后在我的sites_spec.rb中,我将此格式复制为帖子请求的有效参数:

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

这很有效。 params的JSON格式需要在测试块中格式化,就像它是真正的JSON请求一样。