3

テストしようとしている次のようなリンクがあります(角括弧は無視してください):

[%= link_to "ユーザーの削除", destroy_user_account_path(@profile.user), :class => "delete", :confirm => "a", :title => "削除 #{@profile.user.name}", :メソッド => :削除 %]

以下のテストは失敗しますが、 :confirm => "a" 行をコメントアウトすると成功します:

  it "should have a link to delete the user's account (using the destroy_user_account action in the registrations controller)" do
    get :show, :id => @profile
    response.should have_selector("a",
                                  :href => destroy_user_account_path(@profile.user),
                                  :confirm => "a",
                                  :title => "Delete #{@profile.user.name}",
                                  :class => "delete", 
                                  :content => "Delete User")
  end

私の失敗を見よ :(

 Failure/Error: response.should have_selector("a",
   expected following output to contain a <a title='Delete Michael Hartl' class='delete' href='/destroy-user-account/159' confirm='a'>Delete User</a> tag:

この行の実際の html 出力は次のとおりです (角かっこは私のものです)。ここでは、テストが期待する「確認」ではなく、属性として「データ確認」が出力されていることに注意してください。

[a href="/destroy-user-account/159" class="delete" data-confirm="a" data-method="delete" rel="nofollow" title="Michael Hartl の削除"]ユーザーの削除[/ a]

このコンテキストで確認とデータ確認の違いを説明し、このエラーが発生する理由/修正方法を理解するのに役立つ人はいますか?

ありがとう!

4

2 に答える 2

1

「Confirm」オプションは、link_to が提供する「data-confirm」の単なるエイリアスです。

link_to anything, :confirm => "Message" # is equivalent to
link_to anything, 'data-confirm' => "Message"

ただし、使用するマッチャーはエイリアスを認識しないため、そこで「データ確認」を使用する必要があります。

response.should have_selector("a",
                              :href => destroy_user_account_path(@profile.user),
                              'data-confirm' => "a",
                              :title => "Delete #{@profile.user.name}",
                              :class => "delete", 
                              :content => "Delete User")
于 2012-04-20T17:33:07.397 に答える
1

「確認」は HTML 属性ではありません。data-whateverタグは、主にクライアント側で Javascript との間で情報をやり取りするために、必要なカスタム属性を要素に配置できる HTML5 の機能です。

So:<a confirm="foo"></a>は有効な HTML ではありませんが、有効です<a data-confirm="foo"></a>

Rails UJS はdata-confirmタグを探し、クリックすると確認メッセージが表示されることを認識しています。値から確認メッセージを取得しdata-confirmます。

したがって、この場合、コードは次のようになります。

response.should have_selector("a",
                              :href => destroy_user_account_path(@profile.user),
                              'data-confirm' => "a",
                              :title => "Delete #{@profile.user.name}",
                              :class => "delete", 
                              :content => "Delete User")

これで問題は解決するはずですが、そうでない場合はお知らせください。

于 2012-04-20T17:23:35.343 に答える