Ruby-on-rails 如何使用 Capybara 获取带有查询字符串的当前路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5228371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get current path with query string using Capybara
提问by kriysna
The page url is something like /people?search=namewhile I used current_pathmethod of capybara it returned /peopleonly.
页面 url 就像/people?search=name我使用current_path水豚的方法一样,它只返回/people。
current_path.should == people_path(:search => 'name')
But it fails saying
但它没有说
expected: "/people?search=name"
got: "/people"
How we can make it pass? Is there is any way to do this?
我们怎样才能让它通过?有没有办法做到这一点?
回答by nzifnab
I've updated this answer to reflect modern conventions in capybara. I think this is ideal since this is the accepted answer, and what many people are being referred to when looking for a solution. With that said, the correct way to check the current path is to use the has_current_path?matcher provided by Capybara, as documented here: Click Here
我已经更新了这个答案以反映水豚的现代惯例。我认为这是理想的,因为这是公认的答案,也是许多人在寻找解决方案时所参考的答案。话虽如此,检查当前路径的正确方法是使用has_current_path?Capybara 提供的匹配器,如此处所述:单击此处
Example usage:
用法示例:
expect(page).to have_current_path(people_path(search: 'name'))
As you can see in the documentation, other options are available. If the current page is /people?search=namebut you only care that it's on the /peoplepage regardless of the param, you can send the only_pathoption:
正如您在文档中看到的那样,还有其他选项可用。如果当前页面是/people?search=name但您只关心它在/people页面上而不管参数如何,您可以发送only_path选项:
expect(page).to have_current_path(people_path, only_path: true)
Additionally, if you want to compare the entire URL:
此外,如果要比较整个 URL:
expect(page).to have_current_path(people_url, url: true)
Credit to Tom Walpole for pointing out this method.
感谢 Tom Walpole 指出了这种方法。
回答by Robert Starsi
I replaced the _path method with _url to achieve comparing the full urls with parameters.
我用 _url 替换了 _path 方法,以实现将完整 url 与参数进行比较。
current_url.should == people_url(:search => 'name')
回答by Thomas Walpole
Just updating this question for modern times. The current best practice for checking current_paths when using Capybara 2.5+ is to use the current_path matcher, which will use Capybaras waiting behavior to check the path. If wanting to check against the request_uri (path and query string)
只是为现代更新这个问题。当前在使用 Capybara 2.5+ 时检查 current_paths 的最佳实践是使用 current_path 匹配器,它将使用 Capybara 等待行为来检查路径。如果要检查 request_uri(路径和查询字符串)
expect(page).to have_current_path(people_path(:search => 'name'))
If only wanting the path part (ignoring the query string)
如果只想要路径部分(忽略查询字符串)
expect(page).to have_current_path(people_path, only_path: true) # Capybara < 2.16
expect(page).to have_current_path(people_path, ignore_query: true) # Capybara >= 2.16
If wanting to match the full url
如果想匹配完整的 url
expect(page).to have_current_path(people_url, url: true) # Capybara < 2.16
expect(page).to have_current_path(people_url) # Capybara >= 2.16
the matcher will take a string which is compared with == or a regex to match against
匹配器将采用与 == 或正则表达式进行比较的字符串进行匹配
expect(page).to have_current_path(/search=name/)
回答by Lasse Bunk
I know an answer has been selected, but I just wanted to give an alternative solution. So:
我知道已经选择了一个答案,但我只是想提供一个替代解决方案。所以:
To get the path and querystring, like request.fullpathin Rails, you can do:
要获取路径和查询字符串,就像request.fullpath在 Rails 中一样,您可以执行以下操作:
URI.parse(current_url).request_uri.should == people_path(:search => 'name')
You can also do a helper method in your test class (like ActionDispatch::IntegrationTest) like this (which is what I did):
你也可以在你的测试类中做一个辅助方法(比如ActionDispatch::IntegrationTest),就像这样(我就是这样做的):
def current_fullpath
URI.parse(current_url).request_uri
end
Hope this helps.
希望这可以帮助。

