visit 和 current_path 与在 rails 中使用 .should 有什么区别?

What is the difference between visit and current_path and use of .should in rails?

Then /^I should be on the free signup page$/ do
  *current_path.should == free_signup_path* 
end

Then /^I should be on the free signup page$/ do
   *visit free_signup_path* 
end

这两个有什么区别?

您使用 visit 告诉 rspec 转到该路径以验证某些东西是否有效或是否存在。例如:

it 'contains the new sessions form' do
    visit 'sessions/new'
    current_path.should have_content("Sign In")
end

假设我的登录表单有一个按钮或带有文本的任何内容 'sign in' 此测试将通过。

您通常必须声明测试应该转到什么路径才能检测内容。

希望对您有所帮助!

#visit 告诉浏览器转到新的 url 并将向正在测试的应用程序发起获取请求。 #current_path 让您读取浏览器所在的当前位置。

注意:验证当前路径时应停止使用current_path.should == some_path,而应使用page.should have_current_path(some_path)。 have_current_path 匹配器包含等待行为,有助于减少使用支持 JS 的驱动程序的测试