2009-07-29 57 views
1

我正在開發用於shoulda和factory_girl的REST測試。下面用shoulda和factory_girl測試REST - destroy

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. ' 

你能告訴我PLZ - 什麼是錯,以及如何我可以修改測試,以使他們的工作吧?

UPD:路線看起來很好

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

回答

1

也許你應該調用刪除時使用符號和POST方法:

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

(從http://api.rubyonrails.org/classes/ActiveSupport/Testing/Assertions.html#M001427參考)

+0

沒有,一個assert_difference正常工作的新手冊,但你可以在提供的日誌中看到 - 有問題should_redirect_to news_articles_path – 2009-07-29 09:38:53

+0

你能告訴我你的控制器文件? – Lichtamberg 2009-07-29 10:56:16

+0

我想,你刪除後不會被重定向到索引?或者你呢? – Lichtamberg 2009-07-29 10:56:48

5

問題是與should_redirect_to現在使用塊來評估重定向代碼。可悲的是,無論是thinkbot wiki還是github上的自述文件都反映了這一點,並且仍然包含舊的示例。

正確的代碼

should_redirect_to "news articles page" { news_articles_path } 

其中第一個參數只是一個文字描述(它不eval'd與舊版本)用於生成測試名稱,所以你得到一個測試名稱像「應該重定向到新聞文章頁面」

相關問題