I want to test the authentication mechanism, which upon success redirects to the subdomain, by example: 1. The login form is located in http://example.com
2. Users fill in the form and if credentials are correct, is redirected to http://zone.example.com
.
My controller action looks like this:
def create_session
if User.authorize(params[:user])
sign_in user
redirect_to root_url(subdomain: 'zone')
else
redirect_to "#{root_url}#email-login-modal?wrong_password=true"
end
end
The integration test:
context "logging in" do
before(:all) do
@password = 'test123456'
end
let(:user) {create(:user, password: @password)}
scenario "by email", js: true do
visit "/"
page.find("a[href='#email-login-modal']").click
within("#login-form") do
fill_in 'user_email', :with => user.email
fill_in 'user_password', :with => @password
find("button[type='submit']").click
end
expect(page).to have_content user.name
end
end
The login works in development, but the test fails because Capybara does not follow redirection to the subdomain.
这是通过Rails 5和Capybara Chrome驱动程序为我完成的:
Rails.application.routes.default_url_options[:host] = 'lvh.me'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.