水豚fill_in仅适用于字段ID,为什么? [英] Capybara fill_in only works with field id, why?

查看:106
本文介绍了水豚fill_in仅适用于字段ID,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行我的rspec测试时,我遇到一个错误
Capybara :: ElementNotFound:无法找到字段名字

While runnng my rspec tests, I was getting an error Capybara::ElementNotFound: Unable to find field "First Name"

我的rspec测试是如下

My rspec test is as follows

 describe "with valid information" do
  before do
    puts page.body
    fill_in "First Name",   with: "Matthew"
    fill_in "Email",        with: "user@example.com"
    fill_in "Password",     with: "foobar"
  end

  it "should create a user" do
    expect { click_button submit }.to change(User, :count).by(1)
  end
end

我的表单视图是这样的

<%= form_for(@user) do |f| %>
  <%= f.label :first_name %>
  <%= f.text_field :first_name %>

  <%= f.label :email %>
  <%= f.text_field :email %>

  <%= f.label :password %>
  <%= f.password_field :password %>

  <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>

因此,在经过数小时的头部打击之后,我尝试更换

So after a couple hours of head bashing, I tried replacing

fill_in "First Name",   with: "Matthew"

with

fill_in "user_first_name",   with: "Matthew"

(即使用字段ID而不是名称)


我的问题是为什么这只起作用

(i.e. using the field id and not the name)

My question is why does this work only with the field id ('user_first_name') and not the field name ('First Name')?

推荐答案

Rails使用<字段ID('user_first_name')而不是字段名称('Firstname')?

Rails uses the human_attribute_name method to output the label text when you do f.label :first_name.

这意味着标签的文本为名字,而不是名字,这就是您需要提供给Capybara的内容。

This means the text of the label is "First name", not "First Name", so that's what you'll need to provide to Capybara.

这篇关于水豚fill_in仅适用于字段ID,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆