Capybara - 如何忽略click_button之后的表单操作网址?

时间:2016-08-07 19:42:32

标签: ruby-on-rails rspec coffeescript capybara

我有以下表格来创建付款:

付款/ _form.htmlerb

<div class="container">
  <div class="row-fluid">

    <%= form_tag  class: "form-horizontal", id: "payment-form" do %>
      <%= hidden_field_tag "CardRegistrationURL", card_reg['CardRegistrationURL'], id: "CardRegistrationURL" %>
      <%= hidden_field_tag "PreregistrationData", card_reg['PreregistrationData'], id: "PreregistrationData" %>
      <%= hidden_field_tag "AccessKey", card_reg['AccessKey'], id: "AccessKey" %>
      <%= hidden_field_tag "CardId", card_reg['Id'] %>
      <%= hidden_field_tag 'project_url', contract_path(@contract), id: "contractUrlHidden" %>
      <%= hidden_field_tag 'finialize_url', finialize_url, id: "finializeUrl" %>



      <fieldset>
        <div id="legend">
          <legend class="">Deposit for contract <%= @contract.title %></legend>
        </div>

        <div id="legend">
          <legend class="">Amount <%= @contract.amount %></legend>
        </div>

        <!-- Name -->
        <div class="control-group">
          <%= label "card_holder", "Card Holder's Name", class:"control-label" %>
          <div class="controls">
            <%= text_field_tag "card_holder", '', { id: "username", class: "input-xlarge"} %>
          </div>
        </div>

        <!-- Card Number -->
        <div class="control-group">
          <%= label "card_number", "Card number", class:"control-label" %>
          <div class="controls">
            <%= text_field_tag "Alias", '', { id: "card_number", class: "input-xlarge"} %>
          </div>
        </div>

        <!-- Expiry-->
        <div class="control-group">
          <%= label "expity_date", "Card Expiry Date", '', class:"control-label" %>
          <div class="controls">
            <%= text_field_tag "ExpirationDate", '', { id: "card_expiration_date", class: "input-xlarge"} %>
          </div>
        </div>

        <!-- CVV -->
        <div class="control-group">
          <%= label "cvv", "Card CVV", class:"control-label" %>
          <div class="controls">
            <%= text_field_tag "cardCvx", '', { id: "cardCvx", class: "input-xlarge"} %>
          </div>
        </div>

        <!-- Save card -->
        <div class="control-group">
          <div class="controls">
            <label class="checkbox" for="save_card">
              <%= label "save_card", class:"check_box" %>
              <input type="checkbox"> <!-- , '', id="save_card" value="option1"> //-->
              Save card on file?
            </label>
          </div>
        </div>

        <!-- Submit -->
        <div class="control-group">
          <div class="controls">
            <%= submit_tag "Pay now", {class: "btn btn-success", id: "process"} %>
          </div>
        </div>

      </fieldset>
    <% end %>
  </div>
</div>

register_card.coffee

jQuery ->
  $('#payment-form').submit (e) ->
    e.preventDefault()  
    $this = $(this)

    mangoPay.cardRegistration.init
      cardRegistrationURL: $("#CardRegistrationURL").val()
      preregistrationData: $("#PreregistrationData").val()
      accessKey: $("#AccessKey").val()

    cardData = {
      cardNumber: $("#card_number").val()
      cardExpirationDate: $("#card_expiration_date").val()
      cardCvx: $("#cardCvx").val()
    }
    contract_url =  $("#contractUrlHidden").val()
    finialize_url = $("#finializeUrl").val()
    mangoPay.cardRegistration.sendDataWithAjax(
      # URL to capture response
      finialize_url,
      # Card data
      cardData,
      # Result Ajax callback
      (data) ->
        $this.replaceWith('<p> Payment okay</p> </br> <div id="contract_url"><a href="">Return back to project</a></div>')
        $("#contract_url a").attr("href", contract_url)

        #doRedirect = -> window.location.href = data['url']
        #setTimeout doRedirect, 2000

      (xhr, status, error) -> 
        alert("Payment error : " + xhr.responseText + " (" + status + " - " + error + ")")

    )

    return false 

点击按钮“立即付款”将启动脚本register_card.coffee。成功注册卡后查看付款/新将呈现新文本:感谢您的付款。怎么测试这个正确?

我做了以下事情:

expect(current_path).to eq(new_payment_path)
    expect(page).to have_text("Deposit for contract")
    fill_in "card_holder", with: "firstname lastname"
    fill_in "Alias", with: "4706750000000033"
    fill_in "ExpirationDate", with: "1017"
    click_button "Pay now"
    expect(current_path).to eq(new_payment_path)
    expect(page).to have_text("Return back to project")

但是在线点击显示错误click_button“立即付款”。 失败/错误:click_button“立即付款” ActionController的:: RoutingError: 没有路线匹配[POST]“/ payments / new”

我如何忽略[POST]“/ payments / new?使用脚本register_card.coffee中的ajax发送所有数据。

1 个答案:

答案 0 :(得分:0)

您无法使用capybara测试POST请求。基本上,capybara用于测试用户交互,因此如果您手动执行测试表单中描述的步骤,您应该能够创建所需的方案。你不应该检查路径是否是[POST] new_payment_path,而是检查路径是否被重定向到GET页面(我相信它将是索引或show动作)。 此外,如果由ajax完成,则应在scenario子句中使用js:true参数;但是,为此你需要一个javascript驱动程序(我成功地使用Poltergeist来实现这种情况。)