Ruby on rails capybara如何使用href模式测试多个链接
考虑到我有一个包含10个用户的页面, 如何测试我是否有10个用户显示链接 我试过这个:Ruby on rails capybara如何使用href模式测试多个链接,ruby-on-rails,cucumber,bdd,capybara,Ruby On Rails,Cucumber,Bdd,Capybara,考虑到我有一个包含10个用户的页面, 如何测试我是否有10个用户显示链接 我试过这个: Then /^I should see a list of (\d+) users$/ do |num| page.should have_selector('a', {:href=>"users/*", :count => num}) end 这是: Then /^I should see a list of (\d+) users$/ do |num|
Then /^I should see a list of (\d+) users$/ do |num|
page.should have_selector('a', {:href=>"users/*", :count => num})
end
这是:
Then /^I should see a list of (\d+) users$/ do |num|
page.should have_selector('a', {:href=>/users\/\d+/, :count => num})
end
但两者都返回预期的css“a”以返回某些内容(RSpec::Expectations::ExpectationNotMetError)
但是,如果省略:count参数,则测试总是通过:href参数中的任何内容(甚至错误模式)。我将使用xpath和
contains
函数来实现以下目的:
page.should have_xpath("//a[contains(@href,'users')]"), :count => num)
应该将
num
任何a
元素的出现与包含文本“users”的href
属性相匹配。不是一个解决方案,但向链接添加“user”类不是更容易,而不是麻烦使用正则表达式吗?这可能是一个解决方案,但我不想依赖于标记。用户路径是我唯一确信不会在我的页面上更改的路径。