Rswag和Rspec:URL参数的未定义方法

时间:2018-12-14 20:39:28

标签: ruby-on-rails ruby rspec

我正在一个Rails项目中,该项目使用Swagger通过rswag和Rspec进行测试。该代码是一个API,需要通过Rspec系统进行测试。我是rswag和Rspec的新手。

我的代码当前有效,但未通过提供的Rspec测试。我遇到的问题是Rspec报告:

NoMethodError:
   undefined method `comment_id' for #<RSpec::ExampleGroups::Comments::CommentsCommentId::Patch::CommentWithProvidedIdSetToSticky:0x000000000c3055a0>

comment_id是URL中的参数。这是相关的Rswag / rspec定义:

path '/comments/{comment_id}' do
  parameter name: :comment_id, in: :path, type: :string, format: :uuid
  patch 'Sets the sticky comment for a given invoice, client or debtor id' do
    tags 'Comments'
    produces 'application/json'
    consumes 'application/json'
    security [oauth2: []]
    parameter name: :data, in: :body, required: true, schema: {
      type: :object,
      properties: {
        data: {
          type: :object,
          properties: {
            attributes: {
              type: :object,
              required: %i[comment_id],
              properties: {
                sticky_flag: { type: :boolean }
              }
            }
          }
        }
      }
    }

    response '201', 'comment with provided id set to sticky' do
      let(:data) { { comment_id: '', sticky_flag: '' } }
      run_test!
    end

为什么在Rspec输出中出现No Method错误?

2 个答案:

答案 0 :(得分:1)

您需要添加 let(:comment_id){ 'id' } 'id' 您可以通过 FactoryBot 创建记录,例如: let(:comment_id){ FactoryBot.create(:comment).id }

答案 1 :(得分:0)

您的path缺少:,因此ruby将comment_id视为方法或变量,并且不存在。

path '/comments/{comment_id}' do

使用:

path '/comments/{:comment_id}' do