Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting Sinatra's redirect back in rspec
    text
    copied!<p>I am running a sinatra app and have a testing suite setup using <code>rspec 2.7.0</code> and <code>webrat 0.7.3</code> (both the most recent versions). I have an extensive set of tests for all of my request actions and it seems to be working fine. Today I discovered Sinatra's <a href="http://www.sinatrarb.com/intro#Browser%20Redirect" rel="nofollow">redirect back</a> request-level helper and implemented it in a couple of areas of my application that were rendering forms with get requests which were taking parameters.</p> <p>The nice thing about the <code>redirect back</code> helper is that if I have an action say:</p> <pre><code>get '/login' do @used_var = params[:var] haml :login end </code></pre> <p>Which renders a form, I can have validation on the post request receiving the form:</p> <pre><code>post '/login' do # pretend User.authenticate pulls back a user entry from the database if there # is a valid username/password combination unless User.authenticate(params[:username], params[:password]).nil? redirect '/content' else flash[:notice] = "Invalid username/password combo" redirect back # will redirect back to the get '/login' request end end </code></pre> <p>And if the form doesn't validate properly, it will redirect back to the page with the from and retain any parameters that were passed in without me having to worry about storing it to a session variable. The only problem is that rspec doesn't seem to want to play nicely with the <code>redirect back</code> helper. i.e. if I have a spec action that does this:</p> <pre><code>it 'should redirect to login when invalid username/password combo is received.' do get '/login', :var =&gt; 'value' fill_in 'username', :with =&gt; 'invalid_username' fill_in 'password', :with =&gt; 'invalid_password' click_button 'Submit' last_response.should be_redirect; follow_redirect! last_request.url.should include("/login") end </code></pre> <p>The spec fails to pass because for some reason it seems that <code>rspec</code> or <code>webrat</code> isn't picking up on the <code>redirect back</code> helper and is instead redirecting the request back to the <code>root</code> url for my application (<code>'/'</code>).</p> <p>What I want to know is whether there is a way to get rspec to redirect to the proper location in these instances? The actual application functions as expected when I test it with my browser (it redirects me to the first page with parameters), but the rspec tests don't pass properly.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload