Calling routing during a functional test

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.

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

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