Controller code
before_action :verify_request
def verify_request
unless request.headers['Authorization'].present? && request.headers['Authorization'] == APIKEY["authorization_key"]
render json: { message: "UnAuthorize Request" }, status: 401
end
end
Rspec code:
describe "GET #index" do
it "returns 400 (empty query parameter)" do
request.env["HTTP_AUTHORIZATION"] = "testing123"
get :index
expect(response).to have_http_status(400)
end
end
Issue i face is that while rspec testing, environment variables are not accessible in controller and tests are failing. How do i fix that?
Note: I defined authorization_key
in config/authorization.yml for all environments and its working in dev mode but not in testing.
Error I get is:
Failure/Error: unless request.headers['Authorization'].present? && request.headers['Authorization'] == APIKEY["authorization_key"]
NameError:
uninitialized constant V1::LocationController::APIKEY
您是否在CONTROLLER-NAME_controller_spec.rb
包含了require 'rails_helper'
CONTROLLER-NAME_controller_spec.rb
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.