RSpec: simplest test fails with error: Expected block to return true value
I'm testing the waters with Rails and I'm stu开发者_开发百科ck with this simple test:
I have this code in 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
This fails with the following error:
Failure/Error: { :get => '/' }.
Expected block to return true value.
# ./spec/routing/routing_spec.rb:7:in `block (3 levels) in <top (required)>'
What's wrong in my code?
I ran into this opaque error message as well with Rails 3 and Ruby 1.9.2. The reason in my case was that I was using an old version of minitest that ships with Ruby, and in that version the assert_block method (invoked by assert_recognizes in ActionDispatch::Assertions::RoutingAssertions) does not display the error message (how the routing params didn't match). The fix for me was to add minitest to my Gemfile and install it via bundler:
group :test do
gem "minitest", ">= 2.6.1" # The minitest version that ships with Ruby is old and has bugs
end
I would then get a more understandable error message:
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.
This seems like it should work, so my guess would be that there is a configuration problem. Difficult to know what it is, though, without seeing more of the app. Also, the failure says it happens on line 7, but it references line 5 in the code above, so there might be something else in the file that is gumming up the works.
Also, the error message is confusing because the expectation is written on two lines. If you wrote it on one line, you'd see the whole statement. Not that that would help narrow down this problem, other than to help you understand that it's not trying to treat { :get => '/' }
as a block.
HTH, David
This happened with me because my controller rendered the wrong view. I fixed the logic of my controller method and everything worked fine.
精彩评论