我试图在page.current_url
上运行期望,但由于某种原因,page.current_url
未获得更新。
当我点击链接而非访问网址时,会发生。看:
scenario 'When a user who is not an admin visits the dashboard' do
before do
sign_up user
visit admin_statistics_path
end
its(:current_url){ should have_a_uri_of root_path }
end
所有这些规格都通过了。但是,当我点击指向admin_statistics_path的链接而不是访问admin_statistics_path时,current_url期望失败:
describe 'When a user who is not an admin clicks control panel' do
before do
sign_up user
click_link 'dashboard'
# puts current_url
end
it { should have_css 'div.danger', "You do not have sufficient priviledges to access the admin area. Try logging out and logging in with an account that has admin priviledges." }
it { should have_css 'h1', text: 'Goals'}
its(:current_url){ should have_a_uri_of root_path }
end
上述所有期望都远离 current_url 之一!甚至更奇怪,如果我把current_url,规范传递:
describe 'When a user who is not an admin clicks control panel' do
before do
sign_up user
click_link 'dashboard'
puts current_url #=> returns the initial url despite the specs passing
end
it { should have_css 'div.danger', "You do not have sufficient priviledges to access the admin area. Try logging out and logging in with an account that has admin priviledges." }
it { should have_css 'h1', text: 'Goals'}
its(:current_url){ should have_a_uri_of root_path }
end
答案 0 :(得分:2)
对于现在遇到这个问题的人,Capybara 2.5添加了一个has_current_path匹配器,它将在检查current_path时使用Capybaras等待行为
it { should have_current_path(root_path) }
或
expect(page).to have_current_path(root_path)