2011-09-15 12 views
15

Próbuję przetestować przekierowanie na stronie głównej w mojej aplikacji Sinatra (dokładniej, aplikacja Padrino), w rspec. Znalazłem redirect_to, ale wydaje się, że jest tylko w rspec-rails. Jak testujesz to w sinatrze?Jak przetestować przekierowanie w grze sinatra za pomocą programu rspec?

Więc w zasadzie, chciałbym coś takiego:

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 

Odpowiedz

21

Spróbuj tego (nie testowane):

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

Otrzymuję błąd: błąd/Error: follow_redirect! Sequel :: DatabaseError: SQLite3 :: SQLException: brak takiej tabeli: lokalizacje . Zgaduję, że jest to jednak problem z bazą danych. Będzie musiał zajrzeć do tego dalej ... – zlog

+0

Tak, to działa. Dzięki! – zlog

+1

czy możesz mi powiedzieć, gdzie te metody 'last_request',' last_response' są udokumentowane ... W jaki sposób można wywoływać na nich metody takie jak 'url'. Jestem nowy, więc nie mogłem tego dostać. –

15

Więcej bezpośrednio można po prostu użyć 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

W nowej składni expect powinno być:

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 
Powiązane problemy