1

shoulda と factory_girl を使用して REST のテストを開発しています。以下のコード

 context "on :delete to :destroy" do
    setup do
      @controller = NewsArticlesController.new
      @request = ActionController::TestRequest.new
      @response = ActionController::TestResponse.new

      @news_article =  Factory.create(:news_article)

    end

    should "destroy new NewsArticle" do
      assert_difference('NewsArticle.count', -1) do
        delete :destroy, :id => @news_article.id
      end
    end

    should_redirect_to news_articles_path
  end

結果として私は見る

  1) Error:
test: on :delete to :destroy should redirect to index. (NewsArticlesControllerTest):
ArgumentError: block not supplied
    c:/develop/ruby/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/action_controller/macros.rb:201:in `instance_eval'
    c:/develop/ruby/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/action_controller/macros.rb:201:in `__bind_1248853182_16800
0'
    c:/develop/ruby/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `call'
    c:/develop/ruby/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `test: on :delete to :destroy should redirect to index. '

教えてください - 何が問題なのか、テストを修正して正しく動作させるにはどうすればよいですか?

UPD:ルートは問題ないようです

news_articles GET    /news(.:format)                    {:controller=>"news_articles", :action=>"index"}
4

3 に答える 3

5

問題は、should_redirect_toブロックを使用してリダイレクト コードを評価することです。悲しいことに、思考ボット wiki も github の readme もこれを反映しておらず、古い例がまだ含まれています。

正しいコードは

should_redirect_to "news articles page" { news_articles_path }

最初の引数は、テスト名を生成するために使用される単なるテキストの説明 (古いバージョンのように評価されません) であるため、「ニュース記事ページにリダイレクトする必要があります」のようなテスト名を取得します。

于 2009-08-03T11:55:40.787 に答える
1

tkramar ソリューションは正しい方向を示していますが、コードを次のように記述する必要がありました。

should_redirect_to("news articles page") { news_articles_path }

http://dev.thoughtbot.com/shoulda/classes/Shoulda/ActionController/Macros.html#M000015にある新しいマニュアルも参照してください。

于 2009-08-11T13:54:15.580 に答える
1

delete を呼び出すときは、symbol と post メソッドを使用する必要があるかもしれません。

 assert_difference 'Article.count', -1 do
    post :delete, :id => ...
  end

( http://api.rubyonrails.org/classes/ActiveSupport/Testing/Assertions.html#M001427から参照)

于 2009-07-29T09:19:34.687 に答える