# Calling routing during a functional test

**URL:** <https://www.ruby-forum.com/t/calling-routing-during-a-functional-test/62125>\
**Category:** Rails\
**Created:** [May 31, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/calling-routing-during-a-functional-test/62125 "2006-05-31T19:22:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ara-vartanian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b38774/32.png) [@ara-vartanian](https://www.ruby-forum.com/u/ara-vartanian)\
**Post date:** [May 31, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/calling-routing-during-a-functional-test/62125/1 "2006-05-31T19:22:29Z")

</div>

I had a beginner’s question. I am testing the create action on my  
controller. I wanted to fetch the ID parameter of the newly created  
object, so that I can make some assertions on it.

But I can’t seem to find an elegant way of doing that. Of course, might  
create action redirects. That means that @response.redirect\_url is  
something like “/show/8”. So I could pluck the number off that, but that  
seems too brittle, since if I change my routing, that would no longer  
work. I was wondering if there was a way to call routing on the  
redirect\_url, so that I could then fetch the ID from the parameters  
hash. That wouldn’t be brittle at all. But I can’t seem to find any  
documentation for routing, and peeking into the class, I’m not sure what  
the entry points are.

perhaps there’s a much easier way of doing all this.

---

<div class="post-metadata">

**Author:** ![ara-vartanian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b38774/32.png) [@ara-vartanian](https://www.ruby-forum.com/u/ara-vartanian)\
**Post date:** [May 31, 2006, 9:03pm UTC](https://www.ruby-forum.com/t/calling-routing-during-a-functional-test/62125/2 "2006-05-31T21:03:28Z")

</div>

Can you post some code from your Functional test? Based on what  
you’ve typed it sounds like your trying to run Unit/Model assertions  
during a Functional test, which is something you should try to stay  
away from.

Functional tests are how you verify that the “application logic” in  
your controller actions is doing the right thing, returning the  
proper templates/redirects, and rendering the proper tags in your  
XHTML. Running an assertion on a model object is testing “business  
logic”, which is what your Unit tests are for. That doesn’t mean you  
can’t do a model assertion in a functional test, just that doing so  
would be the “wrong place” for that kind of assertion, based on  
everything I’ve seen/read about testing in Rails.

-Brian

---

<div class="post-metadata">

**Author:** ![ara-vartanian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b38774/32.png) [@ara-vartanian](https://www.ruby-forum.com/u/ara-vartanian)\
**Post date:** [May 31, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/calling-routing-during-a-functional-test/62125/3 "2006-05-31T21:39:05Z")

</div>

> I had a beginner’s question. I am testing the create action on my  
> controller. I wanted to fetch the ID parameter of the newly created  
> object, so that I can make some assertions on it.

Why do you need to test the id of a newly created record?

If your create action assigns an instance variable, like this:

@fox = Fox.create

Then you can get at @fox in your tests using the assigns method:

def test\_create\_fox  
post :create, {:name =\> ‘Tall Fox’}  
assert assigns(:fox).name == ‘Tall Fox’  
assert assigns(:fox).id \> 10  
end

–  
Michael D.  
[http://www.mdaines.com](http://www.mdaines.com)
