4

I have a Story resource nested in a User resource. How do I fix this generate functional test?

  test "should create story" do
    assert_difference('Story.count') do
      post :create, story: @story.attributes
    end

    assert_redirected_to story_path(assigns(:story))
  end

DGM solution still have the story_url problem

1 Answer 1

6

You need to provide the user id it is nested in:

post :create, story: @story.attributes, user_id: @user.id

The path may be something like,

user_story_path(@user.id, assigns(:story))
Sign up to request clarification or add additional context in comments.

2 Comments

This is what I get: StoriesControllerTest test_should_create_story ERROR undefined method story_url' for #<StoriesController:0x007fa6094df470> STDERR: Exception NoMethodError' at /Users/Simone/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.1.1/lib/action_dispatch/routing/polymorphic_routes.rb:127:in `polymorphic_url' [...]
Ok it was my fault. redirect_to in #create still use only @story and not [@user, @story]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.