2010-12-13 5 views
1

레일스 3을 배우기 위해서 제 자신의 블로그를 만들고 있습니다. 직접적인 경로가없는 레일즈 컨트롤러 동작 테스트하기

/blog/year/month/slug 

그래서 내 routes.rb이 같은 설정이 있습니다 :

내 URL이를 포맷 할 필요가 웹 브라우저를 통해

Blog::Application.routes.draw do 
    match '/blog', :to => 'post#index' 
    match 'blog/:year/:month/:slug', :to => 'post#show' 
    root :to => 'post#index' 
... 

시험이 잘 작동합니다. 문제는 PostController의 show 조치 메소드를 테스트하려고 할 때 발생합니다.

나는이 테스트 실행 : 내 포스트 컨트롤러에 show 액션 방법을 실행할 수 있도록

ActionController::RoutingError: No route matches {:controller=>"post", :action=>"show"} 

가 어떻게 내 테스트를 작성할 수 있습니다

class PostControllerTest < ActionController::TestCase 
    test "can get a post by slug" do 
    get :show 
    assert_response :success 
    end 
end 

을 나는이 오류가?

답변

2

최소한 year, monthslug 매개 변수가 설정 될 것으로 예상됩니다. 당신의 노선은 그 이상을 필요로합니다. 테스트에서

:이 세 가지 매개 변수와

test "can get a post by slug" do 
    post = Post.create(:slug => "best-post-ever") 
    get :show, :slug => post.slug, :year => Time.now.year, :month => Time.now.month 
    assert_response :success 
end 

, 경로 이제 일치하고 귀하의 요청을 통해 이동합니다.

관련 문제