# Assert\_recognizes example throws syntax error

**URL:** <https://www.ruby-forum.com/t/assert-recognizes-example-throws-syntax-error/173531>\
**Category:** Rails\
**Created:** [August 11, 2009, 4:49am UTC](https://www.ruby-forum.com/t/assert-recognizes-example-throws-syntax-error/173531 "2009-08-11T04:49:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cynthia\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@Cynthia\_K](https://www.ruby-forum.com/u/Cynthia_K)\
**Post date:** [August 11, 2009, 4:49am UTC](https://www.ruby-forum.com/t/assert-recognizes-example-throws-syntax-error/173531/1 "2009-08-11T04:49:11Z")

</div>

I am trying to test some restful routes and the example from the API  
docs  
(suitably munged) is failing. What am I not seeing?

Example from API docs on [api.rubyonrails.com](http://api.rubyonrails.com)

# Check the default route (i.e., the index action)

assert\_recognizes {:controller =\> ‘items’, :action =\> ‘index’},  
‘items’

My test file test/functionals/birds\_controller\_test.rb  
require ‘test\_helper’

class BirdsControllerTest \< ActionController::TestCase  
test “should have some restful routes” do  
assert\_recognizes {:controller =\> ‘birds’, :action =\> “index”},  
“birds”  
end  
end

Test errors

$ ruby -Itest test/functional/birds\_controller\_test.rb  
test/functional/birds\_controller\_test.rb:47: syntax error, unexpected  
tASSOC, expecting ‘}’  
assert\_recognizes {:controller =\> ‘birds’, :action =\> “index”},  
“birds”  
^  
test/functional/birds\_controller\_test.rb:47: syntax error, unexpected  
‘,’,  
expecting ‘}’  
assert\_recognizes {:controller =\> ‘birds’, :action =\> “index”},  
“birds”  
^  
test/functional/birds\_controller\_test.rb:47: syntax error, unexpected  
‘,’,  
expecting kEND  
assert\_recognizes {:controller =\> ‘birds’, :action =\> “index”},  
“birds”  
^

* * *

This is an example app and birds is just created from script/generate  
scaffold birds. And I am using Rails 2.3.2. My routing is as follows:

ActionController::Routing::Routes.draw do |map|  
map.resources :birds  
end

$ rake routes  
birds GET /birds(.:format)  
{:action=\>“index”, :controller=\>“birds”}  
POST /birds(.:format)  
{:action=\>“create”, :controller=\>“birds”  
new\_bird GET /birds/new(.:format) {:action=\>“new”,  
:controller=\>“birds”}  
edit\_bird GET /birds/:id/edit(.:format) {:action=\>“edit”,  
:controller=\>“birds”}  
bird GET /birds/:id(.:format)  
{:action=\>“show”, :controller=\>“birds”}  
PUT /birds/:id(.:format)  
{:action=\>“update”, :controller=\>“birds”}  
DELETE /birds/:id(.:format)  
{:action=\>“destroy”, :controller=\>“birds”}  
/:controller/:action/:id  
/:controller/:action/:id(.:format)

test “should have some restful routes” do  
assert\_routing “/birds/1”, {:controller =\> “birds”, :action =\>  
“show”,  
:id =\> “1”}  
assert\_recognizes {:controller =\> ‘birds’, :action =\> ‘create’},  
{:path  
=\> ‘birds’, :method =\> :post}  
end  
end

–  
Cynthia K.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Cynthia\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@Cynthia\_K](https://www.ruby-forum.com/u/Cynthia_K)\
**Post date:** [August 11, 2009, 11:11am UTC](https://www.ruby-forum.com/t/assert-recognizes-example-throws-syntax-error/173531/2 "2009-08-11T11:11:15Z")

</div>

Hi  
This will work

assert\_recognizes({:controller =\> ‘birds’, :action =\> “index”},“birds”)

Sijo

---

<div class="post-metadata">

**Author:** ![Cynthia\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@Cynthia\_K](https://www.ruby-forum.com/u/Cynthia_K)\
**Post date:** [August 12, 2009, 12:29am UTC](https://www.ruby-forum.com/t/assert-recognizes-example-throws-syntax-error/173531/3 "2009-08-12T00:29:59Z")

</div>

As Sijo pointed out, calls to assert\_recognizes should have  
parentheses around the method parameters when the first parameter is a  
hash. The error you’re seeing is a syntax error in Ruby. The code  
never gets executed because the Ruby parser barfs. The API docs are a  
misleading in this case; assert\_recognizes won’t work with parentheses  
omitted and a hash as the first parameter. Actually, as I poke around  
through the routing\_assertions.rb file, I see several examples of  
omitted parentheses in the docs that may not work exactly as written.

The conventional Ruby wisdom, as far as I can tell is to omit  
parentheses for single method calls that are command-like, such as  
puts. also.omit.parentheses.when.method.chaining. 🙂

Ruby allows parentheses to be omitted whenever possible, but has some  
internal parsing rules that check for ambiguity. I haven’t run across  
a really clear and detailed explanation of these rules, but here’s an  
example:

def my\_method(arg)  
puts ‘hello’ # =\> non-ambiguous, no parentheses needed  
end

my\_method {:a =\> ‘foo’, :b =\> ‘bar’} # =\> ambiguous. Is this a block  
or a hash?

HTH.

-TJ

---

<div class="post-metadata">

**Author:** ![Cynthia\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@Cynthia\_K](https://www.ruby-forum.com/u/Cynthia_K)\
**Post date:** [August 11, 2009, 6:36pm UTC](https://www.ruby-forum.com/t/assert-recognizes-example-throws-syntax-error/173531/4 "2009-08-11T18:36:57Z")

</div>

On Aug 11, 2:11 am, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This will work
> 
> assert\_recognizes({:controller =\> ‘birds’, :action =\> “index”},“birds”)

Thanks. Adding parentheses fixed that test, and more importantly,  
fixed restful routes tests like:

test “should have some restful routes” do  
assert\_recognizes({:controller =\> ‘birds’, :action =\> “index”},  
“birds”)  
assert\_recognizes({:controller =\> ‘birds’, :action =\> ‘create’},  
{:path =\> ‘birds’, :method =\> :post})  
assert\_routing({:path =\> ‘birds’, :method =\> :post}, {:controller  
=\> ‘birds’, :action =\> ‘create’})  
end

Thanks Sijo.
