2008-09-15 5 views
17

की आवश्यकता होती है मैं जो कि शोकहारा प्रमाणीकरण और RSpec शामिल एक आधार अनुप्रयोग है Bort का उपयोग कर एक सीखने आवेदन बनाने के बाद, परीक्षण करने के लिए। मैं इसे शुरू होने के लिए और एक नया उद्देश्य यह है कि उन में लॉग इन करने से पहले वे (नियंत्रक में before_filter :login_required) कुछ भी कर सकते आवश्यकता जोड़ दिया है। [संपादित करें: मुझे यह भी उल्लेख करना चाहिए कि उपयोगकर्ता को has_many नई कक्षा का और केवल उपयोगकर्ता इसे देख पाएगा।]रेल, शोकहारा प्रमाणीकरण और RSpec - कैसे नए मॉडल है कि प्रमाणीकरण

मैंने रुपयेपीसी जेनरेटर का उपयोग करके नया मॉडल/नियंत्रक बनाया है जिसने कई डिफ़ॉल्ट डिफॉल्ट बनाए हैं परीक्षण। वे सब पारित करता है, तो कोई before_filter लेकिन कई, असफल के रूप में उम्मीद की जानी चाहिए, एक बार before_filter स्थान पर है।

मैं कैसे के रूप में अगर कोई है/एक उपयोगकर्ता प्रवेश न होने पर उत्पन्न परीक्षण चलाने के लिए मिलता है? क्या मुझे मेल खाने वाले मिलान के पूरे बैच की आवश्यकता है - रीडायरेक्ट परीक्षण? मुझे लगता है कि यह किसी प्रकार का मॉकिंग या फ़िक्स्चर तकनीक है लेकिन मैं आरएसपीसी और थोड़ा सा उत्साह के लिए नया हूं। अच्छा आरएसपीसी ट्यूटोरियल लिंक भी सराहना की जाएगी।

उत्तर

7

मेरे पास एक बहुत ही समान सेटअप है, और नीचे वह कोड है जिसे मैं वर्तमान में इस सामग्री का परीक्षण करने के लिए उपयोग कर रहा हूं। describe रों में से प्रत्येक में मैं में डाल दिया:

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 
4

मुझे अपने स्वयं के प्रश्न के कुछ जवाब मिल गए हैं। असल में, मुझे यह समझने की आवश्यकता है कि उपयोगकर्ता को restful_authentication से कैसे नकल करना है ताकि स्वत: उत्पन्न आरएसपीसी नियंत्रक परीक्षण पास हो सकें, भले ही मैंने before_filter: login_required जोड़ा था।

यहाँ मेरी सिर्फ पाया संसाधनों में से कुछ हैं:

RSpec: It Should Behave Like

rspec, restful_authentication, and login_required

using restful_authentication current_user inside controller specs

DRYing up your CRUD controller RSpecs

1

एक उपयोगकर्ता नकली करने के लिए में लॉग इन किया जा रहा है, मैं को हैक @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 
7

प्रमाणीकरण परीक्षण नहीं जब लेकिन नियंत्रकों की जरूरत है कि उपयोगकर्ता प्रमाणीकृत करने मैं आमतौर पर फिल्टर विधि ठूंठ परीक्षण:

before(:each) do 
    controller.stub!(:authenticate).and_return(true) 
end 

ऊपर के उदाहरण से काम करता है, जहां मेरे before_filter प्रमाणित विधि पर सेट है :

before_filter :authenticate 

और मेरे ऐप में प्रमाणीकृत HTTP मूल प्रमाणीकरण का उपयोग करता है, लेकिन यह वास्तव में कोई अन्य प्रमाणीकरण तंत्र हो सकता है।

private 
def authenticate 
    authenticate_or_request_with_http_basic do |user,password| 
    user == USER_NAME && password == PASSWORD 
    end 
end 

मुझे लगता है कि यह परीक्षण करने का एक बहुत ही सरल तरीका है।

+0

सबसे प्यारा वाला! – AnkitG

संबंधित मुद्दे