1

名前付きルート (/permalink/terms-of-use) を使用している記事コントローラーでパーマリンク アクションをテストしたい:

map.permalink 'permalink/:permalink', 
              :controller => :articles, :action => :permalink, :as => :permalink

これは仕様です:

describe "GET permalink" do
  it "should visit an article" do
    get "/permalink/@article.permalink"
  end
end

しかし、私はこのエラーが発生します:

ActionController::RoutingError in 'ArticlesController permalink render the page' {:controller=>"articles", :action=>"/permalink/@article.permalink"} に一致するルートがありません

アップデート:

GET の書き方について何か考えはありますか?

4

2 に答える 2

4

このエラーは、コントローラーのいずれかのアクション メソッドの名前を想定しているメソッドに URL 全体を渡しているために発生します。私の理解が正しければ、あなたは一度に複数のことをテストしようとしています。

ルートに名前があることをテストすることは、ルートをテストすることと、コントローラー アクションをテストすることとは異なります。コントローラーのアクションをテストする方法は次のとおりです (これはおそらく当然のことです)。私が使用するものを推奨するのではなく、あなたの命名に一致していることに注意してください。

spec/controllers/articles_controller_spec.rb で、

describe ArticlesController do
  describe '#permalink' do
    it "renders the page" do
      # The action and its parameter are both named permalink
      get :permalink :permalink => 666
      response.should be_success
      # etc.
    end
  end
end

rspec-rails のみを使用して名前付きルートをテストする方法は次のとおりです。

spec/routing/articles_routing_spec.rb で、

describe ArticlesController do
  describe 'permalink' do

    it 'has a named route' do
      articles_permalink(666).should == '/permalink/666'
    end

    it 'is routed to' do
      { :get => '/permalink/666' }.should route_to(
        :controller => 'articles', :action => 'permalink', :id => '666')
    end

  end
end

Shoulda のルート マッチャーはより簡潔ですが、適切な説明と失敗メッセージを提供します。

describe ArticlesController do
  describe 'permalink' do

    it 'has a named route' do
      articles_permalink(666).should == '/permalink/666'
    end

    it { should route(:get, '/permalink/666').to(
      :controller => 'articles', :action => 'permalink', :id => '666' })

  end
end

私の知る限り、RSpec も Shoulda も名前付きルートをテストする具体的で簡潔な方法を持っていませんが、独自のマッチャーを作成することはできます。

于 2011-03-20T20:21:32.907 に答える
0
describe "GET permalink" do
  it "should visit an article" do
    get "/permalink/#{@article.permalink}"
  end
end
于 2011-08-12T10:54:09.487 に答える