2011-08-17 36 views
23

我的页面应该包含一个看起来像<a href="/desired_path/1">Click to go</a>的链接。如何使用assert_select测试给定链接的存在性?

你会如何测试使用assert_select?我想检查是否存在a标记href="/desired_path/1"。你如何测试标签的属性?

是否有任何资源可以解释如何使用assert_select?我阅读了指南和API文档,但没有弄清楚。有没有建议更好的方法来做到这一点?

我正在使用Rails 3并使用内置的测试框架。

谢谢。

回答

13

您可以将任何CSS选择器传递给assert_select。因此,为了测试标签的属性中,您可以使用[attrname=attrvalue]

assert_select("a[href=/desired_path/1]") do |elements| 
    # Here you can test that elements.count == 1 for instance, or anything else 
end 
+0

这不再Rails中的5部作品,也不是,我相信,在导轨4.请参阅[我的答案]( https://stackoverflow.com/a/48394216/187833)为当前的语法。 – jm3

22

在assert_select,你也可以使用一个问号的属性值,然后用一个字符串匹配跟进。

assert_select "a[href=?]", "/desired_path/1"

还有另一种方式来使用assert_select是比较友好的,特别是如果你想匹配的部分字符串或正则表达式模式:

assert_select "a", :href => /acebook\.com\/share/

+4

第二种形式似乎只适用于元素的文本而不适用于属性。它在我尝试时忽略了href条件。 – Shadwell

+0

是的,这个工作,但是,我没有看到任何关于它的文档(除了一个例子和一个传递提及)。 – gamov

+6

正如@Shadwell所说,** 2nd **表单不起作用(Rails 3.2.15)。不幸的是,它不会导致断言失败,我相信:href选项会被忽略。如果有人想自己证明这一点,那么写一些在href正则表达式参数中失败的东西,并看看assert_select是如何传递的。 –

15

这里是你如何可以断言数关于使用assert_select的链接的事情。第二个参数可以是一个字符串或正则表达式来测试对href属性:

# URL string (2nd arg) is compared to the href attribute thanks to the '?' in 
    # CSS selector string. Also asserts that there is only one link matching 
    # the arguments (:count option) and that the link has the text 
    # 'Your Dashboard' (:text option) 
    assert_select '.menu a[href=?]', 'http://test.host/dashboard', 
     { :count => 1, :text => 'Your Dashboard' } 

    # Regular expression (2nd arg) tests the href attribute thanks to the '?' in 
    # the CSS selector string. 
    assert_select '.menu a[href=?]', /\Ahttp:\/\/test.host\/dashboard\z/, 
     { :count => 1, :text => 'Your Dashboard' } 

对于其他方式,你可以使用assert_select,这里有从Rails的ActionPack的所采取的例子3.2.15文档(见文件actionpack-3.2.15/lib/action_dispatch/testing/assertions/selector.rb ):

# At least one form element 
    assert_select "form" 

    # Form element includes four input fields 
    assert_select "form input", 4 

    # Page title is "Welcome" 
    assert_select "title", "Welcome" 

    # Page title is "Welcome" and there is only one title element 
    assert_select "title", {:count => 1, :text => "Welcome"}, 
     "Wrong title or more than one title element" 

    # Page contains no forms 
    assert_select "form", false, "This page must contain no forms" 

    # Test the content and style 
    assert_select "body div.header ul.menu" 

    # Use substitution values 
    assert_select "ol>li#?", /item-\d+/ 

    # All input fields in the form have a name 
    assert_select "form input" do 
    assert_select "[name=?]", /.+/ # Not empty 
    end 
+1

你将如何测试多个属性?像'a [href =?数据富= “酒吧”]]'? – Mohamad

+1

@Mohamad,我认为这样做,每个属性都在它自己的方括号中: 'assert_select'a [href =?] [data-foo =“bar”]','http:// test。主机/仪表板' –

2

对于任何使用来自Rails 4.1并升级到Rails 4.2的assert_select的人。

在4.1这个工作:

my_url = "/my_results?search=hello" 
my_text = "(My Results)" 
assert_select 'a[href=?]', my_url, my_text 

在4.2这给了一个错误:“弃用警告:断言不是因为无效的CSS选择器的运行意想不到的‘(’后“[:平等,” \ “/ my_results \” “]”“

我改变了语法,这和它的工作!:

assert_select 'a[href=?]', my_url, { text: my_text } 

艾略特答案上面帮我,谢谢:)

+0

搜索这个问题的答案无处不在。感谢张贴这! – Kelly

+0

谢谢。在4.2中,第二个参数'my_url'必须是一个字符串。所以,如果你正在检查一个整数,记得使用'to_s'。例如'assert_select'a [foo-id =?]',foo.id.to_s' –

0
assert_select 'a' do |link| 
    expect(link.attr('href').to_s).to eq expected_url 
end 
0

问题专门问道:“你怎么测试[元素]的属性”其他的答案在这里的方式,在当前的Rails/MINITEST不再工作使用assert_select

。按本issue,关于属性内容的断言现在使用这种更XPath的Y“匹配”的语法:

assert_select "a:match('href', ?)", /jm3\.net/ 
相关问题