레일이,평 인증&RSpec-을 테스트하는 방법 새로운 모델을 요구하는 인증
-
09-06-2019 - |
문제
내가 만들어 학습 사용하여 응용 프로그램 Bort, 는 기본 응용 프로그램을 포함하는 편안한 인증 및 RSpec.나는 그것을 실행하고 추가되는 새로운 객체를 필요로 하는 사용자가 로그인하기 전에 그들은 아무것도 할 수 있습니다(before_filter :login_required
컨트롤러에서).[편집:나는 사용자 has_many
의 새로운 클래스고 사용자만 볼 수 있어야 합니다.]
내가 만들어 새로운 모델이/컨트롤러를 사용하여 Rspec 의 발전기 있는 만들어진 숫자의 기본 테스트합니다.그들은 모두 통과하는 경우에 없 before_filter
그러나 여러 가지 실패하,예상되어야 한다면 before_filter
은 장소입니다.
나는 어떻게 생성 된 테스트를 실행하는 경우가 있/지 않는 사용자가 로그인한 사용자?가 필요한 전체의 배치 일치하는 로그인하지 않으로 리디렉션 테스트?나는 가정은 일종의 조롱거나 설비 기술 그러나 나는 새로운 RSpec 과 비트 표류하였습니다.좋은 RSpec 튜토리얼의 링크 것도 감사드립니다.
해결책
나는 아주 비슷한 설정,그리고 아래 코드는 해요 현재 사용하 테스트 및 무료 세면용품 등이 있습니다.에서 각각의 describe
s 에 넣어:
it_should_behave_like "login-required object"
def attempt_access; do_post; end
만약 당신이 필요로하는 모든 로그인 또는
it_should_behave_like "ownership-required object"
def login_as_object_owner; login_as @product.user; end
def attempt_access; do_put; end
def successful_ownership_access
response.should redirect_to(product_url(@product))
end
필요한 경우 소유입니다.물론,도우미는 방법을 변경(아주 작은)의 각 설정이지만,이는 대부분은 당신을 위해 작업을 수행합니다.이것은 내 spec_helper.rb
shared_examples_for "login-required object" do
it "should not be able to access this without logging in" do
attempt_access
response.should_not be_success
respond_to do |format|
format.html { redirect_to(login_url) }
format.xml { response.status_code.should == 401 }
end
end
end
shared_examples_for "ownership-required object" do
it_should_behave_like "login-required object"
it "should not be able to access this without owning it" do
attempt_access
response.should_not be_success
respond_to do |format|
format.html { response.should be_redirect }
format.xml { response.status_code.should == 401 }
end
end
it "should be able to access this if you own it" do
login_as_object_owner
attempt_access
if respond_to?(:successful_ownership_access)
successful_ownership_access
else
response.should be_success
end
end
end
다른 팁
지 않을 때 테스트 인증만을 테스트 컨트롤러가 필요로 하는 사용자를 인증 나는 일반적으로 스텁 필터법:
before(:each) do
controller.stub!(:authenticate).and_return(true)
end
위 예제는 내 before_filter 설정하는 인증 방법:
before_filter :authenticate
과 인증 내 응용 프로그램에서 사용하는 HTTP Basic Authentication,하지만 정말 수 있는 다른 인증 메커니즘이 있습니다.
private
def authenticate
authenticate_or_request_with_http_basic do |user,password|
user == USER_NAME && password == PASSWORD
end
end
나는 그것의 아주 간단한 방법으로 테스트합니다.
내가 찾는 몇 대한 답변을 자신의 질문입니다.기본적으로,필요한 방법을 이해하는 모의 사용자에서 restful_authentication
그래서 자동으로 생성된 rspec 컨트롤러 테스트를 통과 할 수 있도록 했다 추가 before_filter: login_required
.
여기에서 내가 좋아하는 몇 가지를 발견 자원:
rspec,restful_authentication 및 login_required
조롱하는 사용자는 로그인에서,나는 해킹으로 컨트롤러를 설정하는 @current_user
수동:
module AuthHelper
protected
def login_as(model, id_or_attributes = {})
attributes = id_or_attributes.is_a?(Fixnum) ? {:id => id} : id_or_attributes
@current_user = stub_model(model, attributes)
target = controller rescue template
target.instance_variable_set '@current_user', @current_user
if block_given?
yield
target.instance_variable_set '@current_user', nil
end
return @current_user
end
def login_as_user(id_or_attributes = {}, &block)
login_as(User, id_or_attributes, &block)
end
end