我正在开发一个 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/