Ale*_*sev 1 unit-testing ruby-on-rails shoulda factory-bot
我正在使用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
Run Code Online (Sandbox Code Playgroud)
结果我看到了
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. '
Run Code Online (Sandbox Code Playgroud)
你能告诉我PLZ - 什么是错的以及我如何修改测试以使它们正常工作?
UPD:路线看起来很好
news_articles GET /news(.:format) {:controller=>"news_articles", :action=>"index"}
Run Code Online (Sandbox Code Playgroud)
小智 5
问题是should_redirect_to现在使用块来评估重定向代码.可悲的是,无论是思想维基程序还是github上的自述文件都没有反映出这一点,并且仍然包含旧的例子.
正确的代码是
should_redirect_to "news articles page" { news_articles_path }
Run Code Online (Sandbox Code Playgroud)
其中第一个参数只是用于生成测试名称的文本描述(与旧版本不同),因此您将获得一个测试名称,例如"应该重定向到新闻文章页面"