如何使用 Capybara 获取带有查询字符串的当前路径 [英] How to get current path with query string using Capybara

查看:10
本文介绍了如何使用 Capybara 获取带有查询字符串的当前路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

页面 url 类似于 /people?search=name当我使用水豚的 current_path 方法时,它只返回 /people.

The page url is something like /people?search=name while I used current_path method of capybara it returned /people only.

current_path.should == people_path(:search => 'name')

但它没有说

expected: "/people?search=name"
got: "/people"

我们怎样才能让它通过?有没有办法做到这一点?

How we can make it pass? Is there is any way to do this?

推荐答案

我已更新此答案以反映水豚的现代惯例.我认为这是理想的,因为这是公认的答案,也是许多人在寻找解决方案时所参考的答案.话虽如此,检查当前路径的正确方法是使用 Capybara 提供的 has_current_path? 匹配器,如下所述:点击这里

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

示例用法:

expect(page).to have_current_path(people_path(search: 'name'))

正如您在文档中所见,还有其他选项可用.如果当前页面是 /people?search=name 但你只关心它在 /people 页面上而不考虑参数,你可以发送 only_path 选项:

As you can see in the documentation, other options are available. If the current page is /people?search=name but you only care that it's on the /people page regardless of the param, you can send the only_path option:

expect(page).to have_current_path(people_path, only_path: true)

另外,如果你想比较整个 URL:

Additionally, if you want to compare the entire URL:

expect(page).to have_current_path(people_url, url: true)

感谢 Tom Walpole 指出这种方法.

Credit to Tom Walpole for pointing out this method.

这篇关于如何使用 Capybara 获取带有查询字符串的当前路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆