views:

670

answers:

2

I'm trying to make it so when a model is created via created action it redirects to its show action. That part works fine, but I can't get my functional test to behave. These tests have been modified from what the scaffold provides.

  def setup
    @thing = Factory(:thing)
    assert(@thing.id, "Virtual fixture not valid.")
  end

  def test_create_valid
    Thing.any_instance.stubs(:valid?).returns(true)
    post :create
    assert_redirected_to @thing
  end

I'm using factory_girl in setup. When I run my tests I get this:

Expected response to be a redirect to http://test.host/thing/2 but was a redirect to http://test.host/thing/3.

I've done something very similar with my update action in this controller and the test looks the same, but it works. I'm a little confused as to what's going on.

Edit: Maximiliano points out below that this is probably because this creates a new record in the database, so it redirects to that one. How can I find the new record just created with the create post request?

A: 

DISCLAIMER: (I haven't used factory girl yet... so take with a grain of salt) isn't your factory creating the database record? if it's so, you're creating it twice, one time with the factory, another with the controller; then it's normal that the new record has a different id from the factory one...

Maximiliano Guzman
Makes sense. How do I get the new record, so I can check that we redirect to it?
Mark A. Nicolosi
A: 

Figured it out with the help of Max (thannks!) To get the object that is created in the database I have to use assigns():

assert_redirected_to assigns(:thing)

Mark A. Nicolosi