I have a request spec that is trying to test file download functionality in Rails 3.1 for me. The spec (in part) looks like this:
get document_path(Document.first)
logger(response.body)
response.should be_success
It fails with:
Failure/Error: response.should be_success
expected success? to return true, got false
But if I test the download in the browser, it downloads the file correctly.
Here's the action in the controller:
def show
send_file @document.file.path, :filename => @document.file_file_name,
:content_type => @document.file_content_type
end
And my logger gives this information about the response:
<html><body>You are being <a href="http://www.example.com/">redirected</a>.</body></html>
How can I get this test to pass?
Update:
As several pointed out, one of my before_filters was doing the redirect. The reason is that I was using Capybara to login in the test, but not using it's methods for navigating around the site. Here's what worked (partially):
click_link 'Libraries'
click_link 'Drawings'
click_link 'GS2 Drawing'
page.response.should be_success #this still fails
But now I can't figure out a way to test the actual response was successful. What am I doing wrong here.
Most likely, redirect_to
is being called when you run your test. Here's what I would do to determine the cause.
This will tell you how far execution gets before the redirect. Which in turn will tell you what block of code (probably a before_filter
) is redirecting.
If I had to take a guess off the top of my head, I'd say you have a before_filter
that checks if the user is logged in. If that's true, then you'll need to make sure your tests create a logged-in session before you call the login-protected action.