【发布时间】:2015-11-11 09:35:59
【问题描述】:
我试图在 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
- 如何让水豚等待重定向?
- 为什么放置 current_url 会导致预期通过?
- 为什么访问 url 会导致 current_url 上的期望通过?
- 为什么点击链接到一个 url 不会导致 current_url 的期望值通过?
【问题讨论】:
-
1) 如果重定向需要时间,请使用
sleep2)it{ should have_a_uri_of URI.parse(current_url) }3) 您已经通过此its(:current_url){ should have_a_uri_of root_path }将current_url分配给root_path,这就是为什么每次都需要root_path真正的当前网址 -
请修正缩进
标签: ruby-on-rails rspec capybara