require 'rails_helper'
RSpec.describe "User management", :type => :request do
describe "Example::V2::Users" do
describe "GET /api/v2/users/" do
it 'returns status 200, authorized' do
@token = "Bearer 123"
@url = "https://api.example-v2.com/v2/users/me"
@headers = { "AUTHORIZATION" => @token}
get @url, as: :json, headers: {:Authorization => @token}
expect(response.status).to eq 200
end
end
end
end
我正在尝试通过@token
但收到此错误
Failure/Error: get @url, as: :json, headers: {:Authorization => @token}
TypeError: no implicit conversion of nil into String
我可以在没有参数和标头的情况下发出 get 请求,它可以工作,但是一旦我添加参数或标头,我就会收到错误,我什至尝试这样编写它
1 - get @url, {}, { Authorization: @token}
ArgumentError: wrong number of arguments (given 2, expected 1)
2 - get @url, params: {}, headers: { Authorization: @token}
TypeError: no implicit conversion of nil into String
一些聪明人请指出我正确的方向=(。
宝石:
gem 'rspec-rails', '~> 3.8'
gem 'rails', '~> 6.0.2.2'
get @url, as: :json, headers: { Authorization: @token }
这在我的环境中有效,当type: reuqst
.
我建议你可以在GET请求之前使用byebug
并检查@token和@url。
如果一切看起来都不错。
检查get
是否可以向根网址发出请求?
检查rails_helper
是否需要任何可疑文件?
请检查它对我有用。
require 'rails_helper'
RSpec.describe "User management", :type => :request do
describe "PeopleGoal::V2::Users" do
describe "GET /api/v2/users/" do
it 'returns status 200, authorized' do
request.headers["AUTHORIZATION"] = "Basic #{user.id}"
@url = "https://api.peoplegoal-v2.com/v2/users/me"
get @url, format: :json
expect(response.status).to eq 200
end
end
end
end