2011-11-05 102 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] –