published under license CC4-BY
posted in category Software Development & Programming / Rails Testing
posted at 31. Mar '22
Howto Fix Bearer No Method Error in Rswag
The scenario is like this, trying to tell rswag to use bearer token in swagger file. The error is not clear and telling us nothing.
[1] pry(#<Class>)>
An error occurred while loading ./spec/requests/api/articles_spec.rb.
Failure/Error: security [Bearer: []]
NoMethodError:
undefined method `[]=' for nil:NilClass
# /usr/local/bundle/ruby/2.7.0/bundler/gems/rswag-c85fa3b37a18/rswag-specs/lib/rswag/specs/example_group_helpers.rb:22:in `block (2 levels) in <module:ExampleGroupHelpers>'
# ./spec/requests/api/articles_spec.rb:10:in `block (2 levels) in <top (required)>'
# /usr/local/bundle/ruby/2.7.0/bundler/gems/rswag-c85fa3b37a18/rswag-specs/lib/rswag/specs/example_group_helpers.rb:8:in `path'
# ./spec/requests/api/articles_spec.rb:9:in `block in <top (required)>'
# ./spec/requests/api/articles_spec.rb:3:in `<top (required)>'
An error occurred while loading ./spec/requests/api/articles_spec.rb.
Failure/Error: security [Bearer: []]
NoMethodError:
undefined method `[]=' for nil:NilClass
# /usr/local/bundle/ruby/2.7.0/bundler/gems/rswag-c85fa3b37a18/rswag-specs/lib/rswag/specs/example_group_helpers.rb:22:in `block (2 levels) in <module:ExampleGroupHelpers>'
# ./spec/requests/api/articles_spec.rb:10:in `block (2 levels) in <top (required)>'
# /usr/local/bundle/ruby/2.7.0/bundler/gems/rswag-c85fa3b37a18/rswag-specs/lib/rswag/specs/example_group_helpers.rb:8:in `path'
# ./spec/requests/api/articles_spec.rb:9:in `block in <top (required)>'
# ./spec/requests/api/articles_spec.rb:3:in `<top (required)>'
Run options: include {:focus=>true}
Run options: include {:focus=>true}
The correct solution is to have Bearer: []
in the correct block - get
, post
and similar, not in path
:
require 'swagger_helper'
RSpec.describe 'api/articles', type: :request do
path '/api/articles' do
get 'lists articles' do
security [Bearer: []]
consumes 'application/json'
produces 'application/json'
response(200, 'successful') do
run_test! do
expect(JSON.parse(response.body)).to match_array([
{
"title" => "myrtana.sk"
}
])
end
end
end
end
end
Add Comment