Ruby on rails 3 RSpec:最简单的测试失败,错误:预期块返回真值

Ruby on rails 3 RSpec:最简单的测试失败,错误:预期块返回真值,ruby-on-rails-3,testing,rspec,Ruby On Rails 3,Testing,Rspec,我正在用Rails进行测试,我一直在做这个简单的测试: 我在spec/routing/routing_spec.rb中有这个代码 require 'spec_helper' describe "Accessing the root domain" do it "should route to home#index" do { :get => '/' }. should route_to(:controller => 'home', :ac

我正在用Rails进行测试,我一直在做这个简单的测试:

我在spec/routing/routing_spec.rb中有这个代码

  require 'spec_helper'

  describe "Accessing the root domain" do
    it "should route to home#index" do
      { :get => '/' }.
        should route_to(:controller => 'home', :action=>'index')
    end 
 end
此操作失败,出现以下错误:

 Failure/Error: { :get => '/' }.
       Expected block to return true value.
     # ./spec/routing/routing_spec.rb:7:in `block (3 levels) in <top (required)>'
失败/错误:{:get=>'/'}。
预期块将返回真值。
#./spec/routing/routing_spec.rb:7:in'block(3层)in'

我的代码出了什么问题

这似乎应该是可行的,所以我猜可能是存在配置问题。但是,如果不看更多的应用程序,就很难知道它是什么。同样,失败说明它发生在第7行,但它引用了上面代码中的第5行,因此文件中可能有其他东西正在破坏工作

此外,错误消息令人困惑,因为预期写在两行上。如果你把它写在一行,你就会看到整个陈述。这并不是说这将有助于缩小这个问题的范围,而是帮助您理解它不是试图将
{:get=>'/'}
视为一个块

嗯,,
David发生这种情况是因为我的控制器呈现了错误的视图。我修复了控制器方法的逻辑,一切正常。

我在Rails 3和Ruby 1.9.2中遇到了这个不透明的错误消息。在我的例子中,原因是我使用的是Ruby附带的minitest的旧版本,在该版本中,assert_块方法(由assert_在ActionDispatch::Assertions::RoutingAssertions中识别)不显示错误消息(路由参数如何不匹配)。我的解决方案是将minitest添加到我的Gemfile中,并通过bundler进行安装:

group :test do
  gem "minitest", ">= 2.6.1" # The minitest version that ships with Ruby is old and has bugs
end
然后我会得到一个更容易理解的错误消息:

The recognized options <{"action"=>"index",
"controller"=>"publish/product_versions",
"product_id"=>"ipad_app"}> did not match <{"controller"=>"publish/product_versions",
"action"=>"indexx",
"product_id"=>"ipad_app"}>, difference: <{"action"=>"indexx"}>.
Expected block to return true value.
已识别的选项“索引”,
“控制器”=>“发布/产品版本”,
“产品id”=>“ipad应用程序”}>与“发布/产品版本”不匹配,
“action”=>“indexx”,
“产品id”=>“ipad应用程序”}>,区别:“indexx”}>。
预期块将返回真值。
您所说的“错误视图”是什么意思?您到底做了什么来修复它?