2011-11-05 69 views
4

我有一个嵌套在用户资源中的Story资源。 如何解决这个生成功能测试?功能测试和嵌套资源

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解决方案仍然有story_url问题

回答

6

您需要提供用户ID是嵌套在:

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

的路径可能是类似,

user_story_path(@user.id, assigns(:story)) 
+0

这就是我得到的:StoriesControllerTest test_should_create_story错误 未定义的方法'story_url'为# STDERR: 例外'NoMethodError'位于/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'[012] –

+0

好吧,这是我的错。 #create中的redirect_to仍然只使用@story而不是[@user,@ story] –