【问题标题】:Rspec link_to - including confirm/data-confirm attribute causes test to failRspec link_to - 包括确认/数据确认属性导致测试失败
【发布时间】:2012-05-02 07:12:18
【问题描述】:

我有一个链接如下,我正在尝试测试(请忽略方括号):

[%= link_to "删除用户", destroy_user_account_path(@profile.user), :class=> "删除", :confirm => "a", :title => "删除#{@profile.user.name}", :method => :delete %]

下面的测试失败了,但如果我注释掉 :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 输出如下(同样,方括号是我的)。我注意到它在这里输出“data-confirm”作为属性,而不是测试所期望的“确认”。

[a href="/destroy-user-account/159" class="delete" data-confirm="a" data-method="delete" rel="nofollow" title="删除迈克尔 Hartl"]删除用户[/a]

谁能解释在这种情况下确认和数据确认之间有什么区别,并帮助我弄清楚为什么我会收到这个错误/如何解决它?

谢谢!

【问题讨论】:

    标签: ruby-on-rails rspec2 ruby-1.8.7


    【解决方案1】:

    “确认”不是 HTML 属性。 data-whatever 标签是 HTML5 的一项功能,允许您在元素上放置所需的任何自定义属性,主要是在客户端与 Javascript 之间传递信息。

    所以:&lt;a confirm="foo"&gt;&lt;/a&gt; 不是有效的 HTML,但 &lt;a data-confirm="foo"&gt;&lt;/a&gt; 是。

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

    这应该可以解决您的问题,如果没有,请告诉我。

    【讨论】:

    • 谢谢安德鲁。那么确认选项是什么,有什么不同?例如rubydox.net/class/actionpack/2.3.4/…
    • PS:现在测试全部通过。但只是试图理解。
    • 没有“确认”选项,“数据确认”是确认选项。如果您将“确认”传递给 rspec,它将寻找无效标签。
    【解决方案2】:

    “确认”选项只是 link_to 提供的“数据确认”的别名。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多