通过测试Rspec提交表单并获取错误代码422 Unprocessable Entity

时间:2014-10-13 10:28:18

标签: ruby rspec capybara

我正在使用capybara interactive with form

  select_visible(@state, "form label[for='state']+div.field a.chosen-single")
  select_visible(@platform, "form label[for='platform']+div.field a.chosen-single")
  if @appstore_search != nil && @appstore_search != ""
    page.find("#autocomplete_chosen a").click
    page.find("#autocomplete_chosen div.chosen-drop div.chosen-search input[type='text']").set(@appstore_search)
    if page.has_css?("#autocomplete_chosen ul.chosen-results li.active-result")
      page.find("#autocomplete_chosen ul.chosen-results li:nth-child(3)").click
    else
      fill_in('name', :with => @appstore_search)
    end
  else
    fill_in('name', :with => @name)
  end

  click_button 'Save'

  if page.find("form label[for='platform']+div")[:class].include?("field invalid")
    @warning_platform = page.find("form label[for='platform']+div.field.invalid aside.error-message").text
    puts @warning_platform
  end

  if page.find("form label[for='name']+div")[:class].include?("field invalid")
    @warning_name = page.find("form label[for='name']+div.field.invalid aside.error-message").text
    puts @warning_name
  end

  if page.has_css?('#alerts ul li')
    @message = page.find("#alerts ul li").text
    puts @message
  end

我可以填写所有文字框,但我无法发送fill_in('name', :with => @appstore_search)

我收到了错误

    {
    "status": 422,
    "result": {
        "error": "Invalid parameter 'name' value nil: Must be String"
    }
}

有人能建议我解决方案吗?

1 个答案:

答案 0 :(得分:0)

我无法发表评论,所以这不是一个很好的答案,但这是我要做的调试这个(是的,摆脱这些ifs,你应该对不同的场景进行单独的测试)。

   select_visible(@state, "form label[for='state']+div.field a.chosen-single")
   select_visible(@platform, "form label[for='platform']+div.field a.chosen-single")
   puts "appstore_search: #{@appstore_search}"
   puts "name: #{@name}"
      if @appstore_search != nil && @appstore_search != ""
        puts 'appstore_search is present'
        page.find("#autocomplete_chosen a").click
        page.find("#autocomplete_chosen div.chosen-drop div.chosen-search input[type='text']").set(@appstore_search)
        if page.has_css?("#autocomplete_chosen ul.chosen-results li.active-result")
          puts 'page has autocomplete css'
          page.find("#autocomplete_chosen ul.chosen-results li:nth-child(3)").click
        else
          puts 'filling in name with App Store search'
          fill_in('name', :with => @appstore_search)
        end
      else
        puts 'filling in name with name'
        fill_in('name', :with => @name)
      end
      save_and_open_page
      click_button 'Save'

希望所有这些看跌期权都可以帮助您了解代码的进展情况。然后,在单击保存之前,保存和打开页面将以可视方式显示名称框中的内容。