2011-09-15 14 views
15

rspec'te benim sinatra uygulamasında (özellikle, bir padrino uygulaması) ana sayfada bir yönlendirmeyi test etmeye çalışıyorum. redirect_to'u buldum, ancak sadece rspec raylarında gibi görünüyor. Sinatra'da nasıl test edersiniz?rspec kullanarak sinatra'da bir yönlendirmeyi nasıl test edebilirim?

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 

cevap

21

(test) bu deneyin:

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

hata alıyorum: Başarısızlık/Hata: follow_redirect! Sequel :: DatabaseError: SQLite3 :: SQLException: böyle bir tablo yok: konumlar . Sanırım bunun bir veritabanı sorunu var. Bu daha içine bakmak gerekecek ... – zlog

+0

Evet, bu çalışır. Teşekkürler! – zlog

+1

, "last_request", "last_response" yöntemlerinin nerede belgelendiğini bana söyleyebilir misiniz? Bu türlerde 'url' gibi yöntemleri nasıl arayabiliyorsunuz? Ben yeniyim, bu yüzden alamadım. –

15

Daha doğrudan sadece last_response.location kullanabilirsiniz

Yani temelde, böyle bir şey istiyorum. Yeni expect sözdiziminde

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

olması gerektiği:

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 
İlgili konular