我有一个如下链接,我正在尝试测试(请忽略方括号):
[%= link_to“删除用户”,destroy_user_account_path(@ profile.user), :class => “删除”,:confirm => “a”,:title => “删除#{@profile.user.name}”,:method => :删除%]
下面的测试失败,但如果我注释掉: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]
任何人都可以解释在此上下文中确认和数据确认之间的区别,并帮助我弄清楚为什么我会收到此错误/如何修复它?
谢谢!
答案 0 :(得分:1)
&#34;确认&#34;不是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 :(得分: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")