使用rspec测试嵌套路由

时间:2011-05-09 00:01:48

标签: ruby-on-rails rspec routes

我正在尝试使用rspec测试路由。以下给出了“预期块返回真值”的错误。

我不确定我错过了什么。通过浏览器,我可以发布到此网址,并且成功。

有什么想法吗?谢谢!

路线

resources :forum_topics do
  resources :forum_sub_topics
end

测试:

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

2 个答案:

答案 0 :(得分:10)

没有人正式回答我的问题,所以我会:)

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

归结为:forum_topic_id => 1不等于:forum_topic_id => “1”也许我的PHP日子已经悄悄地咬我。字符串和整数:)

答案 1 :(得分:8)

我知道它归结为同样的事情,但如果您想缩短代码,您也可以这样做:

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

我发现它更容易阅读。