【问题标题】:Functional test and nested resource功能测试和嵌套资源
【发布时间】:2011-11-05 16:53:42
【问题描述】:

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

  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问题

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 testing


    【解决方案1】:

    您需要提供它所嵌套的用户 ID:

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

    路径可能类似于,

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

    【讨论】:

    • 这是我得到的: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' [...]
    • 好吧,这是我的错。 #create 中的 redirect_to 仍然只使用 @story 而不是 [@user, @story]
    猜你喜欢
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多