1

ここで気が狂いそうです。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

スクリーンショットとプットは、何が起こっているのかを明確にするためにあります. 基本的に 2 つの異なるケースがあります。の

  1. フィールドをそのままにして、デフォルトの ID (「post_name」と「post_content」) をテストします。明らかに、上記のテストはケース 2 用です。
  2. フィールド ID を「name_here」と「content_here」に変更して、ラベル名やその他のものが干渉しているかどうかをテストします。上記のスペックのものです。

ケース 2 では、カピバラがどちらのフィールドも見つけられないため、投稿が拒否されます。

ケース 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)>'

ケース 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>

post_content フィールドが明確に存在することに注意してください。両方が失敗した場合 (ケース 1)、content_here フィールドと name_here フィールドの両方があります。だから畑があります。そして、カピバラは一般的に機能しています(この種の機能は、私のアプリの他の部分で見つかります)。また、問題はラベル名との競合ではありません。入力フィールドに別の ID を設定しても、カピバラがそれらを見つけるのに役立たないからです。

ちなみに、これは実際にはすべて完全に機能します。

ここで何が起こっているのか、まったくわかりませんか?私はとてもイライラしています/無知です。

更新-- user2172816 のアドバイスに従って、HTML が次のようになるように HAML を変更しました。

<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>

テストは次のようになります。

 # 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')

しかし、それでもエラーが発生します (同じ仕様の新しい部分でも!):

  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)>'
4

1 に答える 1

5

label の "for" 属性は、input 要素の "id" でなければなりません。つまり、

<label for="name_here">Name</label>
   <br><input id="name_here" name="post[name]" size="30" type="text">

仕様では、次のように呼び出す必要があります。

fill_in 'Name', with: "Post Name Here"
于 2013-03-15T06:30:56.183 に答える