How to test for a redirect with Rspec and Capybara

后端 未结 5 989
北恋
北恋 2021-01-01 13:27

I don\'t know what I\'m doing wrong, but every time I try to test for a redirect, I get this error: \"@request must be an ActionDispatch::Request\"

context \         


        
5条回答
  •  不思量自难忘°
    2021-01-01 13:39

    Here is hackish solution that i found

    # spec/features/user_confirmation_feature.rb
    
    feature 'User confirmation' do
      scenario 'provide confirmation and redirect' do
        visit "/users/123/confirm"
    
        expect(page).to have_content('Please enter the confirmation code')
        find("input[id$='confirmation_code']").set '1234'
    
        do_not_follow_redirect do
          click_button('Verify')
          expect(page.driver.status_code).to eq(302)
          expect(page.driver.browser.last_response['Location']).to match(/\/en\//[^\/]+\/edit$/)
        end
      end
    
      protected
    
      # Capybara won't follow redirects
      def do_not_follow_redirect &block
        begin
          options = page.driver.instance_variable_get(:@options)
          prev_value = options[:follow_redirects]
          options[:follow_redirects] = false
    
          yield
        ensure
          options[:follow_redirects] = prev_value
        end
      end
    end
    

提交回复
热议问题