testing nested routes with rspec

2019-06-15 04:29发布

问题:

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.