Rails Capybara没有看到田野

Sas*_*sha 1 rspec ruby-on-rails capybara ruby-on-rails-3

我觉得我在这里疯了.对我的Post模型进行例行的水豚测试.具体来说,确保新页面在有效提交后重定向到新创建的帖子.所以在我的spec/requests文件夹中,我有这个:

describe "Valid post submission --" do

    it "should log in a user and redirect to new post" do

      # Sign in works find, so I won't reproduce it

      # Make a blogpost
      Post.destroy_all
      fill_in :name_here, with: "Post Name Here"
      fill_in :content_here, with: "This is the content of a really short post."
      screenshot_and_open_image
      click_on "Post It"
      screenshot_and_open_image
      puts current_path
      page.should have_selector 'h2', text: "prohibited this post"
      page.should have_selector 'h1', text: "Post Name Here"
      page.should have_selector '.alert', text: "Post was successfully created."
      page.should have_selector 'title', text: full_title('Post Name Here')

    end
Run Code Online (Sandbox Code Playgroud)

截图和看跌期权是为了澄清发生了什么.基本上有两种不同的情况.在

  1. 我只是留下字段并测试默认ID("post_name"和"post_content"); 显然,上述测试适用于案例二:
  2. 我将字段ID更改为"name_here"和"content_here"以测试标签名称或其他任何内容是否存在干扰.这就是上面的规范.

在案例2中,该帖子被拒绝,因为Capybara无法找到任何一个字段.

在案例1中,只有内容为空,但名称字段将填充内容.

案例1中的确切错误是:

2) Posts Invalid post - should refresh with error if content empty
     Screenshot: /Users/myusername/rails/appname/tmp/capybara/screenshot_2013-03-14-22-37-36.634.png
     Failure/Error: fill_in :post_name, with: "Post Name Here"
     Capybara::ElementNotFound:
       cannot fill in, no text field, text area or password field with id, name, or label 'post_name' found
     # (eval):2:in `fill_in'
     # ./spec/requests/posts_spec.rb:43:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

案例2没有引发关于找不到合适内容的错误,我相信因为标签也有该ID.

这太荒谬了,因为我在测试之前拍摄了HTML的快照,就在它出错之前.这是案例2 - 除了ID差异外,这两种情况在这里看起来都是90%相同:

<form accept-charset="UTF-8" action="/posts" class="new_post" id="new_post" method="post">
 <div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="?"></div>
 <div class="field">
  <label for="post_name">Name</label>
   <br><input id="name_here" name="post[name]" size="30" type="text">
 </div>
 <div class="field text-area">
  <label for="post_content">Content</label>
  <br><textarea cols="50" id="content_here" name="post[content]" rows="20"></textarea>
 </div>
 <div class="actions btn-group">
  <input class="btn" name="commit" type="submit" value="Post It">
 </div>
</form>
Run Code Online (Sandbox Code Playgroud)

请注意post_content字段的明确存在.如果两者都失败(案例1),则同时存在content_here和name_here字段.所以田地都在那里.而水豚一般都在工作(这种东西可以在我的应用程序的其他部分找到).并且问题不是与标签名称冲突,因为在输入字段上放置不同的id并不能帮助capybara找到它们.

顺便说一句,这一切都在现实中完美运作.

根本不知道这里发生了什么?我非常沮丧/无能为力.

更新 - 按照user2172816的建议,我更改了我的HAML,因此HTML看起来像这样:

<form accept-charset="UTF-8" action="/posts" class="new_post" id="new_post" method="post">
 <div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="?"></div>
 <div class="field">
  <label for="post_name">Name</label>
  <br><input id="name" name="post[name]" size="30" type="text">
 </div>
 <div class="field text-area">
  <label for="post_content">Content</label>
  <br><textarea cols="50" id="content" name="post[content]" rows="20"></textarea>
 </div>
 <div class="actions btn-group">
  <input class="btn" name="commit" type="submit" value="Post It">
 </div>
</form>
Run Code Online (Sandbox Code Playgroud)

测试现在看起来像这样:

 # Make a blogpost
  Post.destroy_all
  fill_in "Name", with: "Post Name Here"
  fill_in "Content", with: "This is the content of a really short post."
  page.should have_selector 'h2', text: "prohibited this post"
  page.should have_selector 'h1', text: "Post Name Here"
  page.should have_selector '.alert', text: "Post was successfully created."
  page.should have_selector 'title', text: full_title('Post Name Here')
Run Code Online (Sandbox Code Playgroud)

但它仍然是错误的(同样规格的新部分也是如此!):

  1) Posts Invalid post - should refresh with error if content empty
     Screenshot: /Users/username/rails/appname/tmp/capybara/screenshot_2013-03-14-23-54-38.072.png
     Failure/Error: fill_in :name, with: "Post Name Here"
     Capybara::ElementNotFound:
       cannot fill in, no text field, text area or password field with id, name, or label 'name' found
     # (eval):2:in `fill_in'
     # ./spec/requests/posts_spec.rb:43:in `block (3 levels) in <top (required)>'

  2) Posts Invalid post - should refresh with error if name empty
     Screenshot: /Users/username/rails/appname/tmp/capybara/screenshot_2013-03-14-23-54-38.274.png
     Failure/Error: fill_in :name, with: ""
     Capybara::ElementNotFound:
       cannot fill in, no text field, text area or password field with id, name, or label 'name' found
     # (eval):2:in `fill_in'
     # ./spec/requests/posts_spec.rb:33:in `block (3 levels) in <top (required)>'

  3) Posts Valid post submission -- should log in a user and redirect to new post
     Screenshot: /Users/username/rails/appname/tmp/capybara/screenshot_2013-03-14-23-54-38.459.png
     Failure/Error: fill_in :name, with: "Post Name Here"
     Capybara::ElementNotFound:
       cannot fill in, no text field, text area or password field with id, name, or label 'name' found
     # (eval):2:in `fill_in'
     # ./spec/requests/posts_spec.rb:67:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

小智 5

label的属性"for"应该是input元素的"id",即

<label for="name_here">Name</label>
   <br><input id="name_here" name="post[name]" size="30" type="text">
Run Code Online (Sandbox Code Playgroud)

在规范中你应该打电话:

fill_in 'Name', with: "Post Name Here"
Run Code Online (Sandbox Code Playgroud)