fill_in在Rspec / Capybara中如何工作? [英] How does fill_in work in Rspec/Capybara?

查看:69
本文介绍了fill_in在Rspec / Capybara中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注Michael Hartl的Ruby on Rails教程。当我使用rspec / capybara时,fill_in方法使我感到困惑。我有以下查看代码:

I am following Michael Hartl's Ruby on Rails Tutorial. When I use rspec/capybara, the fill_in method makes me confused. I have the following view code:

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

这是我的测试代码:

 fill_in "Name", with: "Example User"

它似乎fill_in定位输入字段都需要label和text_field。如果我摘下 f.label 或将<%= f.text_field:name%> 更改为<%= f.text_field:another_name%> ,测试将给我 ElementNotFound 错误。有人可以在这里解释fill_in的工作方式吗? fill_in 方法是否需要输入字段和标签?

It seems that label and text_field are both required for fill_in to locate the input field. If I either take off f.label or change <%= f.text_field :name %> to be <%= f.text_field :another_name %>, the test will give me ElementNotFound error. Can anyone explain how the fill_in works here? Are input field and label both required for fill_in method?

推荐答案

它被声明表示 fill_in 查找字段 name id 或标签文本。根据Rails指南的 ActionView :: Helpers :: FormHelper 部分,查看您要查询的代码应转换为以下html代码:

It is stated that fill_in looks for field name, id or label text. According to ActionView::Helpers::FormHelper section of rails guides, the view code which you ask about should be translated to the following html code:

# I assume that you made a form for a @user object
<label for="user_name">
  Name
</label>
<input id="user_name" name="user[name]" type="text" />

如您所见,标签产生了名称文本,您可以在 fill_in 表达式中要求输入。但是 input 字段的 id name 属性稍有不同,因此您应该一直使用基于 id 的选择器来达到相同的结果:

As you see, label produced the "Name" text, which you ask for inside of your fill_in expression. But id and name properties of input field are slightly different, so you should have been using id based selector to achieve the same result:

fill_in user_name,加上:'示例用户'

总而言之,标签字段不是必需的,但是您应该仔细查看html代码,并为 fill_in 表达式选择适当的参数。

So, to sum up, label field is not required, but you should watch for your html code carefully and select the appropriate parameters for fill_in expression.

这篇关于fill_in在Rspec / Capybara中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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