【问题标题】:Functional Testing Rails, No route matches error功能测试 Rails,没有路线匹配错误
【发布时间】:2014-12-06 23:19:12
【问题描述】:

我正在尝试修复以下 rails 功能测试:

test "should create broadcast" do
  login_as(:one_details)
  assert_difference('Broadcast.count') do
    post :create, feeds: [:twitter, {alumni_email: 'test@email.com'}], broadcast: {id: 2, content: @broadcast.content, user: @user_details.user_id}
  end
  assert_redirected_to "#{broadcast_path}?page=1"
end

这是错误:

1) Error:
BroadcastsControllerTest#test_should_create_broadcast:
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"broadcasts"} missing required keys: [:id]
    test/controllers/broadcasts_controller_test.rb:35:in `block in <class:BroadcastsControllerTest>'

我在路由文件中有这个:

resources :broadcasts, except: [:edit, :update]

在 broadcasts_controller 中,show 方法如下所示:

def show
  if is_admin?
    respond_to do |format|

      format.js { render partial: 'show_local',
                       locals: {broadcast: @broadcast, current_page: @current_page},
                       layout: false }
      format.html # show.html.erb
      format.json # show.json.builder
    end
  else
    indicate_illegal_request I18n.t('not allowed to view this')
  end
end

广播控制器创建方法:

定义创建

  @broadcast = Broadcast.new(broadcast_params)

  # Wire up broadcast with the current user (an administrator)
  # Will be an admin user (see before_filter)
  # Note the current_user is a user_detail object so we need
  # to navigate to its user object
  @broadcast.user = current_user.user

  # Doing the next line forces a save automatically. I want to defer this
  # until the "if" statement
  #current_user.user.broadcasts << @broadcast

  no_errors = false
  respond_to do |format|
    if @broadcast.save

      # Only after saving do we try and do the real broadcast. Could have been
      # done using an observer, but I wanted this to be more explicit

      results = BroadcastService.broadcast(@broadcast, params[:feeds])
      if results.length > 0
        # Something went wrong when trying to broadcast to one or more of the
        # feeds.
        @broadcast.errors[:base] << ("#{I18n.t('broadcasts.unable-message')}: #{results.inspect}")
        flash[:error] = I18n.t('broadcasts.saved-but-message')
      else
        flash[:notice] = I18n.t('broadcasts.saved-message')
        no_errors = true
      end
      if no_errors
        format.html { redirect_to(broadcasts_url(page: @current_page)) }
        format.json { render json: @broadcast, status: :created, location: @broadcast }
      else
        format.html { render :new }
        format.xml {
          # Either say it partly worked but send back the errors or else send
          # back complete failure indicator (couldn't even save)
          if results
            render json: @broadcast.errors, status: :created, location: @broadcast
          else
            render json: @broadcast.errors, status: :unprocessable_entity
          end
        }
      end
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails testing ruby-on-rails-4 functional-testing


    【解决方案1】:

    改变

     assert_redirected_to "#{broadcast_path}?page=1"
    

      assert_redirected_to "#{broadcasts_path}?page=1"
    

    也变了

    post :create, feeds: [:twitter, {alumni_email: 'test@email.com'}], broadcast: {id: 2, content: @broadcast.content, user: @user_details.user_id}
    

    post :create, feeds: [:twitter, {alumni_email: 'test@email.com'}], broadcast_params: {id: 2, content: @broadcast.content, user: @user_details.user_id}
    

    您没有在重定向检查中传递 id,因此您要么将其设为 broadcasts_path

    【讨论】:

    • 如果你修改了从哪里得到的行? Page=1 不在我的建议中?
    • 您是否保存了测试文件?
    • 是的,我的测试现在看起来像这样: test "should create broadcast" do login_as(:one_details) assert_difference('Broadcast.count') do post :create, feeds: [:twitter, {alumni_email: 'test@email.com'}],广播:{内容:@broadcast.content,用户:@user_details.user_id} end assert_redirected_to broadcast_path(assigns(:broadcast)) #assert_response :success end
    • 你是如何运行你的测试的?如果错误消息相同,则它没有捕捉到那些最新的更改,您的错误中不再显示 ?page=1。
    • 我正在使用 - rake test:functionals 从命令行运行测试
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多