登录用户尝试访问新操作和创建操作后,尝试在页面重定向后测试通知。
规范失败:
失败:
1) Authentication authorization as signed in master submitting a GET request to the Masters#new action
Failure/Error: it { should have_content("You cannot complete this request when signed in.") }
Capybara::ElementNotFound:
Unable to find xpath "/html"
# ./spec/requests/authentication_pages_spec.rb:126:in `block (5 levels) in <top (required)>'
2) Authentication authorization as signed in master submitting a POST request to the Masters#create action
Failure/Error: it { should have_content("You cannot complete this request when signed in.") }
Capybara::ElementNotFound:
Unable to find xpath "/html"
# ./spec/requests/authentication_pages_spec.rb:132:in `block (5 levels) in <top (required)>'
过滤前:
before_action :already_signed_in, only: [:new, :create]
def already_signed_in
redirect_to root_url, notice: "You cannot complete this request when signed in." if signed_in?
end
授权规范:
context "as signed in master" do
let(:master) { FactoryGirl.create(:master) }
before { sign_in master, no_capybara: true }
subject { page }
context "submitting a GET request to the Masters#new action" do
before { get new_master_path }
specify { expect(response).to redirect_to(root_url) }
it { should have_content("You cannot complete this request when signed in.") }
end
context "submitting a POST request to the Masters#create action" do
before { post masters_path }
specify { expect(response).to redirect_to(root_url) }
it { should have_content("You cannot complete this request when signed in.") }
end
end
最佳答案
嗯,是啊!响应没有任何内容,因为它是重定向!
所以你可以询问flash
对象是否有消息。
如果您确实想知道它是否显示在下一页上,您需要编写集成测试,因为它实际上会遵循浏览器中的重定向并检查后续页面中的消息。
关于ruby-on-rails - 使用 Rspec 测试页面重定向后的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20595885/