Shoulda, Factory_Girl and PaperClip Test Problem

21 views
Skip to first unread message

Bharat

unread,
Jun 23, 2009, 8:54:12 AM6/23/09
to shoulda
I am down to the last test for my controllers. This one baffles me:

In my DocumentController class I have the following method for
downloading documents:

class DocumentsController < ApplicationController

before_filter :login_required
before_filter :current_practice_and_folder_selection_required
...
def download_document
@document = Document.find(params[:id])
send_file(@document.doc.path(:original))
end

end

Corresponding Shoulda test is shown below:

require 'test_helper'

class DocumentsControllerTest < ActionController::TestCase

context 'on GET to :download a document record' do
setup do
@practice_user = Factory(:practice_user)
@document = Factory(:document)
get :download_document, {:id => @document.id},
{:user_id => @practice_user.id,
:practice_id => @document.folder.practice_id,
:folder_id => @document.folder_id}
end
should_assign_to :document
should_respond_with :success
end

end

This, however, does not work, and responds with the following error
messages:

1) Error:
on GET to :download a document record should assign @document
(DocumentsControllerTest):
ActionController::MissingFile: Cannot read file /home/bruparel/work/
ssfilemgr/public/system/docs/1/original/../../public/images/bl.png
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/streaming.rb:78:in `send_file'
/home/bruparel/work/ssfilemgr/app/controllers/documents_controller.rb:
53:in `download_document'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/base.rb:1322:in `send'

2) Error:
on GET to :download a document record should respond with success
(DocumentsControllerTest):
ActionController::MissingFile: Cannot read file /home/bruparel/work/
ssfilemgr/public/system/docs/2/original/../../public/images/bl.png
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/streaming.rb:78:in `send_file'

The application works fine. The test does not, it errors at the
following line of code of the method download_document:

def download_document
@document = Document.find(params[:id])
send_file(@document.doc.path(:original)) <---- here
end

I am using PaperClip for document upload/download.

Bharat

Bharat

unread,
Jun 24, 2009, 4:46:46 PM6/24/09
to shoulda
Thank you Jon. It works:

context 'on GET to :download a document record' do
setup do
@practice_user = Factory(:practice_user)
@document = Factory(:document)
@document.doc = File.new("#{RAILS_ROOT}/test/functional/bl.png")
@document.save
get :download_document, {:id => @document.id},
{:user_id => @practice_user.id,
:practice_id => @document.folder.practice_id,
:folder_id => @document.folder_id}
end
should_assign_to :document
should_respond_with :success
end

Regards,

Bharat
Reply all
Reply to author
Forward
0 new messages