使用 RSpec 测试 Rails API 控制器 POST

awe*_*use 4 ruby testing api rspec ruby-on-rails

正如标题所示,我只是想用 RSpec 测试我的 API 控制器中的创建操作。控制器看起来像:

module Api
  module V1
    class BathroomController < ApplicationController
      skip_before_action :verify_authenticity_token, only: [:create]`

      def create
        bathroom = Bathroom.new(bathroom_params)
        bathroom.user = current_user
        if bathroom.save
          render json: { status: 'SUCCESS', message: 'Saved new bathroom', bathrooms: bathroom }, status: :ok
        end
      end

      private
      def bathroom_params
        params.require(:bathroom).permit(:establishment, :address, :city, :state, :zip, :gender, :key_needed, :toilet_quantity)
      end

    end
  end
end
Run Code Online (Sandbox Code Playgroud)

现在这正是它应该做的,这很棒。然而,测试......不是那么多。这是我对测试部分的了解:

describe "POST #create" do
  let!(:bath) {{
    establishment: "Fake Place",
    address: "123 Main St",
    city: "Cityton",
    state: "NY",
    zip: "11111",
    gender: "Unisex",
    key_needed: false,
    toilet_quantity: 1
  }}
  let!(:params) { {bathroom: bath} }
  it "receives bathroom data and creates a new bathroom" do
    post :create, params: params

    bathroom = Bathroom.last
    expect(bathroom.establishment).to eq "Fake Place"
  end
end
Run Code Online (Sandbox Code Playgroud)

我确信这里有不止一件事错误,但我很难找到有关测试此正确方法的大量信息。任何见解或建议将不胜感激。

max*_*max 9

我会完全跳过控制器规格。Rails 5 几乎已将ActionController::TestCase(RSpec 包装为控制器规范)委托给垃圾抽屉。控制器测试不会发送真正的 http 请求,也不会对 Rails 的关键部分(如路由器和中间件)进行存根。很快就会发生完全的贬低和委托给一个单独的宝石。

相反,您想使用请求规范。

RSpec.describe "API V1 Bathrooms", type: 'request' do
  describe "POST /api/v1/bathrooms" do
    context "with valid parameters" do
      let(:valid_params) do
        {
           bathroom: {
            establishment: "Fake Place",
            address: "123 Main St",
            city: "Cityton",
            state: "NY",
            zip: "11111",
            gender: "Unisex",
            key_needed: false,
            toilet_quantity: 1
          }
        }
      end

      it "creates a new bathroom" do
        expect { post "/api/v1/bathrooms", params: valid_params }.to change(Bathroom, :count).by(+1)
        expect(response).to have_http_status :created
        expect(response.headers['Location']).to eq api_v1_bathroom_url(Bathroom.last)
      end

      it "creates a bathroom with the correct attributes" do
         post "/api/v1/bathrooms", params: valid_params
         expect(Bathroom.last).to have_attributes valid_params[:bathroom]
      end
    end

    context "with invalid parameters" do
       # testing for validation failures is just as important!
       # ...
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

同样发送一堆垃圾 render json: { status: 'SUCCESS', message: 'Saved new bathroom', bathrooms: bathroom }, status: :ok是一种反模式。

作为响应,您应该只发送带有位置标头的 201 CREATED 响应,其中包含指向新创建资源的 url 或包含新创建资源的响应正文。

def create
  bathroom = current_user.bathrooms.new(bathroom_params)
  if bathroom.save
    head :created, location: api_v1_bathroom_url(bathroom)
  else
    head :unprocessable_entity
  end     
end
Run Code Online (Sandbox Code Playgroud)

如果您的客户无法通过查看响应代码判断响应是否成功,那么您做错了。