2010-12-13 8 views
1

rails3を学ぶために自分のブログを構築しています。Railsコントローラのアクションを直接ルートなしでテストする

にフォーマットする私のURLが必要です。だから私は、私のroutes.rbをこのように設定してい

/blog/year/month/slug 

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

テストこのWebブラウザを介して素晴らしい作品。私のPostControllerのshowアクションメソッドをテストしたいときに問題が発生します。私はこのテストを実行

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

私はこのエラーを取得する:私は私のポストコントローラでshowアクションメソッドを実行することができるように

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

は、私は私のテストを書くことができますどのように?

答えて

2

これは少なくとも、year,monthおよびslugパラメータが設定されることを期待しています。あなたのルートはそれ以上のものが必要です。あなたのテストで

:これら三つのパラメータで

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 

、ルートは現在、マッチされ、リクエストが通過します。

関連する問題