页面url类似于/people?search=name
,而我使用的是current_path
方法,它只返回/people
。
current_path.should == people_path(:search => 'name')
但它没能说
expected: "/people?search=name"
got: "/people"
我们怎么才能让它过去?有什么办法吗?
发布于 2011-03-07 20:40:36
我更新了这个答案,以反映海原的现代习俗。我认为这是理想的,因为这是公认的答案,也是许多人在寻找解决办法时所提到的问题。尽管如此,检查当前路径的正确方法是使用Capybara提供的has_current_path?
匹配器,如本文所述:单击此处
示例用法:
expect(page).to have_current_path(people_path(search: 'name'))
正如您在文档中所看到的,其他选项是可用的。如果当前页面是/people?search=name
,但您只关心它在/people
页面上,而不管param是什么,则可以发送only_path
选项:
expect(page).to have_current_path(people_path, only_path: true)
此外,如果您想比较整个URL:
expect(page).to have_current_path(people_url, url: true)
汤姆·沃尔波尔指出了这一方法,值得赞扬。
发布于 2011-11-20 08:52:26
我将_path方法替换为_url,以实现对完整_url与参数的比较。
current_url.should == people_url(:search => 'name')
发布于 2015-11-20 08:26:29
只是为了现代才更新这个问题。当前在使用Capybara current_paths时检查2.5+的最佳实践是使用current_path matcher,后者将使用Capybaras等待行为检查路径。如果希望检查request_uri (路径和查询字符串)
expect(page).to have_current_path(people_path(:search => 'name'))
如果只想要路径部分(忽略查询字符串)
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
如果想要匹配完整的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
匹配器将使用与==或正则表达式相比较的字符串进行匹配。
expect(page).to have_current_path(/search=name/)
https://stackoverflow.com/questions/5228371
复制