首页 文章

Rspec link_to - 包括confirm / data-confirm属性导致测试失败

提问于
浏览
3

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

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

下面的测试失败,但如果我注释掉:confirm =>“a”行,它会通过:

它“应该有一个删除用户帐户的链接(使用注册控制器中的destroy_user_account操作)”
get:show,:id => @profile
response.should have_selector(“a”,
:href => destroy_user_account_path(@ profile.user),
:confirm =>“a”,
:title =>“删除#{@profile.user.name}”,
:class =>“删除”,
:content =>“删除用户”)
结束

看我的失败:(

失败/错误:response.should have_selector(“a”,
预计以下输出包含<a title='Delete Michael Hartl'class='delete'href='/destroy-user-account/159'confirm='a'>删除用户</a>标签:

此行的实际html输出如下(同样,方括号是我的) . 我注意到它输出“data-confirm”作为此处的属性,而不是测试期望的“确认” .

[a href =“/ destroy-user-account / 159”class =“delete”data-confirm =“a”data-method =“delete”rel =“nofollow”title =“删除Michael Hartl”删除用户[ /一个]

任何人都可以解释在这种情况下确认和数据确认之间的区别,并帮助我找出为什么我收到此错误/如何解决它?

谢谢!

2 回答

  • 1

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

    所以: <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")
    

    这应该照顾你的问题,如果没有,请告诉我 .

  • 1

    “确认”选项只是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")
    

相关问题