微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Rails 请求规范 - nil:NilClass (rswag) 的未定义方法“body”

如何解决Rails 请求规范 - nil:NilClass (rswag) 的未定义方法“body”

我正在尝试使用 rwsag 生成 API 文档,但我对我的一些规范失败感到困惑。

这是我的请求规范:-

require 'swagger_helper' ### this includes 'rails_helper'

RSpec.describe 'api/v1/users',type: :request do
  before(:each) { host! 'localhost:3001' }

  path '/api/v1/users' do

    post('create user') do
      tags 'users'
      consumes 'application/json'
      parameter name: :user,in: :body,schema: {
        type: :object,properties: {
          title: { type: :string },description: { type: :string },date: { type: :datetime },budget: { type: :decimal },awarded: { type: :boolean }
        },required: [ 'title','description' ]
    }
      response(200,'successful') do

        after do |example|
          example.Metadata[:response][:content] = {
            'application/json' => {
              example: JSON.parse(response.body,symbolize_names: true)
            }
          }
        end
        run_test!
      end
    end
  end

以下是我认为没有多大意义的两个错误:-

1) api/v1/users /api/v1/users post successful returns a 200 response
     Got 0 failures and 2 other errors:

     1.1) Failure/Error: super
          
          NoMethodError:
            undefined method `user' for #<RSpec::ExampleGroups::ApiV1Users::ApiV1Users::Post::Successful:0x0000aaaad7d06600>
          # /usr/local/bundle/gems/rswag-specs-2.4.0/lib/rswag/specs/request_factory.rb:197:in `build_json_payload'
          # /usr/local/bundle/gems/rswag-specs-2.4.0/lib/rswag/specs/request_factory.rb:180:in `add_payload'
          # /usr/local/bundle/gems/rswag-specs-2.4.0/lib/rswag/specs/request_factory.rb:22:in `block in build_request'
          # /usr/local/bundle/gems/rswag-specs-2.4.0/lib/rswag/specs/request_factory.rb:18:in `tap'
          # /usr/local/bundle/gems/rswag-specs-2.4.0/lib/rswag/specs/request_factory.rb:18:in `build_request'
          # /usr/local/bundle/gems/rswag-specs-2.4.0/lib/rswag/specs/example_helpers.rb:10:in `submit_request'
          # /usr/local/bundle/gems/rswag-specs-2.4.0/lib/rswag/specs/example_group_helpers.rb:94:in `block in run_test!'

     1.2) Failure/Error: example: JSON.parse(response.body,symbolize_names: true)
          
          NoMethodError:
            undefined method `body' for nil:NilClass
          # ./spec/requests/api/v1/users_spec.rb:84:in `block (5 levels) in <main>'

还有我的标准控制器动作:-

module Api
  module V1
    class UsersController < ApplicationController
# POST /api/v1/users
      def create
        @user = Job.new(user_params)

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

知道是什么导致了这个错误吗?我也对生成的 rswag 规范结构有点困惑 - 即任何地方都没有 expects

解决方法

答案是我错过了下面的 let(:user) 行:-

  response(201,'successful') do

    after do |example|
      example.metadata[:response][:content] = {
        'application/json' => {
          example: JSON.parse(response.body,symbolize_names: true)
        }
      }
    end
    let(:user) { { title: 'foo',description: 'bar' } }
    run_test!
  end

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?