Devise: 4.20
Rails: 5.0.1
Rspec: 3.5
ich diesen Link https://github.com/plataformatec/devise/wiki/How-To:-Use-HTTP-Basic-Authentication benutzt hatte, aber ich havproblems die http Grund Auth für meine api testen für Anfragen Tests mit rspec. Unten ist das Beispiel der Fehler:Probleme für die Anforderung rspec mit grundlegenden http auth mit Entwickeln
app/Controller/api/base_controller.rb
module Api
class BaseController < ApplicationController
before_action :authenticate_user!
protected
def authenticate_user!
authenticate_or_request_with_http_basic do |username, password|
resource = User.find_by_username(username)
if resource
sign_in :user, resource if resource.valid_password?(password)
else
request_http_basic_authentication
end
end
end
end
end
app/Controller/api/v1/car_controller.rb
module Api
module V1
class CarController < Api::BaseController
respond_to :json
def index
@cars = Car.all
render :json => {:content => @cars}, :status => 200
end
end
end
end
spec/Anfragen/api /v1/car_controller_spec.rb
require 'rails_helper'
RSpec.describe "Servers API", :type => :request do
it 'sends a list of servers' do
admin = FactoryGirl.create(:admin)
@env = {}
@env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(admin.username, admin.password)
get "/api/cars", :params => {}, :headers => @env
# test for the 200 status-code
expect(response.status).to eq(200)
end
end
Wenn ich die spec laufen, ich habe die folgenden Fehler:
# --- Caused by: ---
# NoMethodError:
# undefined method `sign_in' for #<Api::V1::CarController:0x0000000609ef12>
# ./app/controllers/api/base_controller.rb:10 in block in authenticate_user!
Jeder kann mir helfen? Vielen Dank.