ruby-on-rails - Rswag 和 Rspec : Undefined method for URL parameter

标签 ruby-on-rails ruby rspec

我正在开发一个 Rails 项目,该项目通过 rswag 和 Rspec 使用 Swagger 进行测试。代码是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 错误?

最佳答案

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

关于ruby-on-rails - Rswag 和 Rspec : Undefined method for URL parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53786609/

相关文章:

ruby-on-rails - 如何对任意列表使用 ensure_inclusion_of

ruby-on-rails - RSpec:主页的集成测试

ruby-on-rails - Rails 4 ForbiddenAttributesError - 嵌套资源

Ruby:从模块中将多个方法作为 proc 返回的更好方法

ruby - 覆盖 session Controller 防止使用自定义 View

css - Rails 3.1 中的 CSS 样式有什么问题?

ruby - 如何在共享上下文之间共享 RSpec let 变量?

ruby-on-rails - rails : Is there any way to build dynamic role based authorization in rails?

javascript - 如何在客户端管理当前用户 session ?

mysql - 防止 Rails 缓存 ActiveRecord 查询的结果