【问题标题】:Is it possible to check if the `form` is submitting to the proper link?是否可以检查“表单”是否提交到正确的链接?
【发布时间】:2012-03-19 02:44:41
【问题描述】:

我正在使用 Ruby on Rails 3.2.2、cucumber-rails-1.3.0、rspec-rails-2.8.1 和 capybara-1.1.2。通过使用 Capybara,我想检查 HTML form 是否提交到正确的 URL;也就是说,检查相关的action="<PATH>" HTML "tag"/"property" 是否是我所期望的。

例如,在以下代码中,我想检查<PATH> 是否为/users(完整的HTML 为action="/users"),而Ruby on Rails 路由为new_user_path

<form method="post" id="css_form_id" action="/users">
  ...
</form>

此时,为了检查页面上是否存在表单,我正在使用以下代码:

Then /^I should see the form$/ do
  page.should have_selector('form#css_form_id', :visible => true)
end

是否可以检查form 是否提交到正确的 URL?如果是这样,我该怎么做?你有什么建议?

【问题讨论】:

    标签: ruby-on-rails ruby rspec cucumber capybara


    【解决方案1】:

    我认为您缺少行为驱动开发的基本概念。您真的关心表单的名称或发送数据的位置吗?当您谈论应用程序的高级行为时,不是这样。您应该测试应用程序是否正在执行提交表单时应该执行的操作。而不是检查它发布到哪个路径,您应该测试提交表单时,结果是预期的结果。看起来您正在使用此表单创建一个新用户,所以我会检查是否已使用您提交的参数创建了一个新用户:

    When I create the following user:
      | Name  | Some Person      |
      | Email | person@email.com |
    Then I should have the following user:
      | Name  | Some Person      |
      | Email | person@email.com |
    

    然后,您的断言步骤将检查用户是否已在步骤之间持久化

    Then /^I should have the following user:$/ do |user_table|
      expected_user = user_table.rows_hash
      user = User.find_by_email(expected_user['Email'])
      user.name.should == expected_user['Name']
    end
    

    不这样测试您的视图的另一个原因是因为它使您的测试过于脆弱。如果您更改表单的类或 id,即使行为根本没有改变,您的测试也会中断。

    【讨论】:

      猜你喜欢
      • 2014-06-13
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 2012-02-21
      • 2014-06-09
      相关资源
      最近更新 更多