사용자가 로그인 한 후 적절한 페이지로 리디렉션되도록 테스트를 작성하려고합니다. 그러나 문제는 사용자가 확인하려는 페이지에 도착하기 전에 두 번의 리디렉션을 거친 것입니다. 현재 expect(subject).to redirect_to(accounts_plan_page)
을 사용하면 첫 번째 리디렉션에서 리디렉션 체인이 중지되고 마지막 페이지는 계속되지 않습니다. 따라서 로그인 흐름은 localhost/signin -> localhost/ -> localhost/account/plan
이됩니다. 사용자가 /account/plan
에 도착했지만 redirect_to가 루트 경로에서 중지되는지 테스트하고 싶습니다. rspec이 첫 번째로 끝나는 대신 최종 리디렉션을 따르도록 할 여지가 있습니까?Rspec redirect_to 여러 번의 리디렉션을 따르십시오
단축 login_spec는 :
describe 'login', type: :request do
let(:user) { FactoryGirl.create(:user) }
subject { post '/signin', { user: { email: user.email, password: user.password } } }
it 'redirects a user properly' do
accounts_plan_page = 'http://127.0.0.1:3000/account/plan'
if user.account.requires_metered?
expect(subject).to redirect_to(account_plans_page)
end
end
end
이것은 당신은 당신의 테스트를 실행 post
을 post_via_redirect
을 대신 사용하여 리디렉션을 수행 SPEC 요청을 할 수 있습니다
Expected response to be a redirect to <http://127.0.0.1:3000/account/plan> but was a redirect to <http://127.0.0.1:3000/>
감사합니다. 나는 이것으로 일을 멋지게 할 수 있었다. 나는'response.original_url'을보고 올바른 페이지에 착륙했는지 결정했다. 이 방법이 가장 좋은 방법입니까 아니면 다른 것이 있습니까? – user3663139
나는 괜찮다고 생각한다. 또한 Rspec의 기능 테스트를 검토하여 완전한 브라우저 상호 작용을 시뮬레이션 할 수 있습니다. – dpassage