Redirecting functional testing in a Rails controller

I am trying to do this, when the model is created through the generated action, it is redirected to the show action. This 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 am using factory_girl in setup. When I run my tests, I get this:

The expected answer is a redirect to http://test.host/thing/2 , but was redirected to http://test.host/thing/3 .

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

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

0


a source to share


2 answers


Figured it out with Max (thannks!) To get the object created in the database I need to use assigns ():



assert_redirected_to assign (: thing)

+1


a source


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



0


a source







All Articles