我正在使用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"}发布于 2009-07-29 09:19:35
也许你应该在调用delete时使用符号和post方法:
assert_difference 'Article.count', -1 do
post :delete, :id => ...
end(引用自http://api.rubyonrails.org/classes/ActiveSupport/Testing/Assertions.html#M001427)
发布于 2009-08-03 11:55:41
问题出在should_redirect_to,它现在使用块来评估重定向代码。遗憾的是,thoughtbot wiki和github的自述文件都没有反映这一点,仍然包含旧的示例。
正确的代码是
should_redirect_to "news articles page" { news_articles_path }其中第一个参数只是用于生成测试名称的文本描述(与旧版本不同),因此您会得到一个类似“应该重定向到新闻文章页面”的测试名称。
发布于 2009-08-11 13:54:16
tkramar解决方案指向了正确的方向,但我不得不将代码写成:
should_redirect_to("news articles page") { news_articles_path }另请参阅http://dev.thoughtbot.com/shoulda/classes/Shoulda/ActionController/Macros.html#M000015上的新手册
https://stackoverflow.com/questions/1198742
复制相似问题