访问和current_path和使用.should在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 

这两个有什么区别?访问和current_path和使用.should在rails中有什么区别?

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

注意:在验证当前路径时,应停止使用current_path.should == some_path,而应使用page.should have_current_path(some_path)。 has_current_path匹配器包含等待行为,并将有助于使测试更轻松与JS功能的驱动程序

+0

谢谢汤姆的帮助。 – tanay

您可以使用visit来告诉rspec转到该路径以验证某些工作是否有效。例如:

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

当然,我的登录表单有一个按钮或任何文本“登录”,这个测试会通过。

您通常必须声明测试应该通过什么路径来检测内容。

希望这会有所帮助!