2011-09-15 13 views
15

मैं rspec में अपने sinatra ऐप (अधिक विशेष रूप से, एक पैड्रिनो ऐप) में होमपेज पर रीडायरेक्ट के लिए परीक्षण करने का प्रयास कर रहा हूं। मुझे redirect_to मिला है, हालांकि यह केवल आरएसपीसी-रेल में ही प्रतीत होता है। आप sinatra में इसके लिए कैसे परीक्षण करते हैं?मैं rspec का उपयोग कर sinatra में एक रीडायरेक्ट का परीक्षण कैसे करूं?

it "Homepage should redirect to locations#index" do 
    get "/" 
    last_response.should be_redirect # This works, but I want it to be more specific 
    # last_response.should redirect_to('/locations') # Only works for rspec-rails 
    end 

उत्तर

21

इस प्रयास करें (परीक्षण नहीं):

it "Homepage should redirect to locations#index" do 
    get "/" 
    last_response.should be_redirect # This works, but I want it to be more specific 
    follow_redirect! 
    last_request.url.should == 'http://example.org/locations' 
end 
+0

मैं त्रुटि मिल रही है: विफल/त्रुटि: follow_redirect! सीक्वेल :: डाटाबेस त्रुटि: SQLite3 :: SQLException: ऐसी कोई तालिका नहीं: स्थान । मुझे लगता है कि यह एक डेटाबेस मुद्दा है हालांकि। इसे आगे देखने की आवश्यकता होगी ... – zlog

+0

हाँ, यह काम करता है। धन्यवाद! – zlog

+1

क्या आप मुझे बता सकते हैं कि उन 'last_request', 'last_response' विधियों को दस्तावेज किया गया है ... आप उन पर' url 'जैसी विधियों को कैसे कॉल कर सकते हैं। मैं नया हूं, इसलिए इसे नहीं मिला। –

15

अधिक सीधे तुम सिर्फ last_response.location उपयोग कर सकते हैं

तो बुनियादी तौर पर, मैं कुछ इस तरह करना चाहते हैं।

it "Homepage should redirect to locations#index" do 
    get "/" 
    last_response.should be_redirect 
    last_response.location.should include '/locations' 
end 
1

नई expect वाक्य रचना में यह होना चाहिए:

it "Homepage should redirect to locations#index" do 
    get "/" 
    expect(last_response).to be_redirect # This works, but I want it to be more specific 
    follow_redirect! 
    expect(last_request.url).to eql 'http://example.org/locations' 
end