代码之家  ›  专栏  ›  技术社区  ›  Antarr Byrd

来自RSpec的意外状态代码

  •  0
  • Antarr Byrd  · 技术社区  · 7 年前

    :created 应在何时显示状态 :unprocessable_entity .

      def update
        head :unprocessable_entity unless announcement
        if annnouncement.update(announcement_params)
          render json: annoucement, status: :ok
        else
          render_unprocessable_entity
        end
      end
    
      def announcement
        Announcement.find(params[id])
      end
    

    announcements\u controller\u规范

    it 'fails when announcement does not exists' do
      data = {
        id: 999_999,
        announcement: {
          body: ''
        }
      }.to_json
      post "/api/announcements/#{property.slug}", params: data, headers: headers
      expect(response).to have_http_status(:unprocessable_entity)
    end
    

      1) Api::AnnouncementsController#update fails when announcement does not exists
         Failure/Error: expect(response).to have_http_status(:unprocessable_entity)
           expected the response to have status code :unprocessable_entity (422) but it was :created (201)
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   mrzasa    7 年前

    您使用 post 而不是 put #create 控制器操作编号 #update

    顺便说一句,你写道:

    Announcement.find(params[id])
    

    缺少冒号:

    Announcement.find(params[:id])