2012-06-27 29 views
31

Sadece RSpec ve Cabybara'nın ne kadar havalı olduğunu öğrendim ve şimdi gerçek testi yazmayı öğrenmek için etrafta çalışıyorum.RSpec ve Capybara (Rails) ile Test Yönlendirme

Bir bağlantıyı tıkladıktan sonra, belirli bir sayfaya yönlendirme olup olmadığını kontrol etmeye çalışıyorum. Aşağıda

 

1) I have a page /projects/list 
     - I have an anchor with html "Back" and it links to /projects/show 

Below is the test i wrote in rspec 

describe "Sample" do 
    describe "GET /projects/list" do 
    it "sample test" do 
     visit "/projects/list" 
     click_link "Back" 
     assert_redirected_to "/projects/show" 
    end 
    end 
end 

testi

 
    Failure/Error: assert_redirected_to "/projects/show" 
    ArgumentError: 
     @request must be an ActionDispatch::Request 

i yanlış yapıyorum yönlendirmeyi test etmek ve gerektiği konusunda beni önermek Lütfen aşağıdaki gibi bir hata mesajı ile başarısız senaryodur?

cevap

68

current_path.should == "/projects/show"

Capybara da tam olarak nitelenmiş bir URL için current_url yöntemi uygular deneyin.

Daha fazla bilgi için docs.

+3

'project_path' kullanmak daha iyi olabilir mi? Ve belki de 'bekle' notasyonu kullanırken, – SuckerForMayhem

-2

Sen rotayı kullanmak gerekir, gibi bir şey:

assert_redirected_to projects_path 

yerine

Devise wiki itibaren
assert_redirected_to "/projects/show" 
+0

i assert_redirected_to o 'tanımlanmamış yerel değişken veya yöntem' projects_path' gibi gösterir projects_path' 'gibi kullandığınızda # ' – balanv

+0

için project_show_path bu durumda –

4

: Rails

zaman raf uygulaması yönlendirmeleri (tıpkı Warden/Devise sizi giriş sayfasına yönlendirir), yanıt entegrasyon oturumu tarafından doğru şekilde güncellenmez. Sonuç olarak, assert_redirected_to işe yaramaz.

Rspec make sure we ended up at correct path Yani daha doğrusu bunun yönlendiriliyorsunuz teste göre, bu URL şimdi olduğunuzu test etmek gerekir:

Ayrıca, bu sayfayı aynı bilgileri yer alır.

10

Im emin değilim bu İhtiyacınız ama benim testlerde böyle bir yaklaşım tercih neyi olabilir:

... 
subject { page } 
... 
before do 
    visit some_path_path 
    # do anything else you need to be redirected 
end 
it "should redirect to some other page" do 
    expect(page.current_path).to eq some_other_page_path 
end 
+0

'page.current_path' gereksizdir, zaten' page 'bir konu olarak bildirdiğinizden beri' current_path 'kullanabilirsiniz – mitra

İlgili konular