testing nested routes with rspec

喜欢而已 提交于 2019-12-23 07:01:38

问题


I'm trying to test routes with rspec. The following gives an error of "Expected block to return true value".

I'm not sure what I am missing. Through a browser I can post to this url and it is successful.

Any ideas? Thanks!

Routes

resources :forum_topics do
  resources :forum_sub_topics
end

Test:

it "recognizes and generates nested #create" do
  { :post => "/forum_topics/1/forum_sub_topics" }.should route_to(:controller => "forum_sub_topics", :action => "create", :forum_topic_id => 1)
end

回答1:


Well nobody officially answered my question, so I will :)

{ :post => "/forum_topics/1/forum_sub_topics" }.should route_to(:controller => "forum_sub_topics", :action => "create", :forum_topic_id => 1)

It comes down to :forum_topic_id => 1 not being equal to :forum_topic_id => "1" Perhaps my PHP days have crept up to bite me. Strings and Integers :)




回答2:


I know it comes down to the same thing but if you'd like to shorten your code you could also do:

{ :post => "/forum_topics/1/forum_sub_topics" }.should route_to("forum_sub_topics#create", :forum_topic_id => 1)

I find it easier to read.



来源:https://stackoverflow.com/questions/5931235/testing-nested-routes-with-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!