Ruby on Rails Rspec:你如何检查当前路径?

Jul*_*lia 2 rspec ruby-on-rails

检查当前路径是否为特定路径的代码是什么?

Sim*_*tti 5

您有多种选择,具体取决于您在控制器或视图中。

在视图中,您可以使用current_page?helper

current_page?(action: 'process')
# => false

current_page?(controller: 'shop', action: 'checkout')
# => true

current_page?(controller: 'shop', action: 'checkout', order: 'asc')
# => false

current_page?(action: 'checkout')
# => true

current_page?(controller: 'library', action: 'checkout')
# => false

current_page?('http://www.example.com/shop/checkout')
# => true

current_page?('/shop/checkout')
# => true
Run Code Online (Sandbox Code Playgroud)

在控制器中,您通常会检查request.path字符串。


kdd*_*isz 5

您可以使用变量current_path,如

expect(current_path).to eq '/users'
Run Code Online (Sandbox Code Playgroud)