# Send\_file testing

**URL:** <https://www.ruby-forum.com/t/send-file-testing/161049>\
**Category:** RSpec\
**Created:** [February 22, 2009, 9:59pm UTC](https://www.ruby-forum.com/t/send-file-testing/161049 "2009-02-22T21:59:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vo.x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/45deac/32.png) [@vo.x](https://www.ruby-forum.com/u/vo.x)\
**Post date:** [February 22, 2009, 9:59pm UTC](https://www.ruby-forum.com/t/send-file-testing/161049/1 "2009-02-22T21:59:38Z")

</div>

Hello all,

Is there some best practice how to test Rails controller action which  
is using send\_file method? Could you help me please?

Vit

---

<div class="post-metadata">

**Author:** ![vo.x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/45deac/32.png) [@vo.x](https://www.ruby-forum.com/u/vo.x)\
**Post date:** [February 23, 2009, 8:44am UTC](https://www.ruby-forum.com/t/send-file-testing/161049/2 "2009-02-23T08:44:55Z")

</div>

Just to be clear, this is how the action looks:

def download  
log = ActivityLog.find(params[:id])  
if log  
send\_file log.path  
else  
flash[:notice] = \_(“Log file “#{params[:id]}” was not found.”)  
redirect\_to :action =\> ‘index’  
end  
end

Vit

---

<div class="post-metadata">

**Author:** ![vo.x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/45deac/32.png) [@vo.x](https://www.ruby-forum.com/u/vo.x)\
**Post date:** [February 23, 2009, 7:17pm UTC](https://www.ruby-forum.com/t/send-file-testing/161049/3 "2009-02-23T19:17:09Z")

</div>

On 22/02/2009, at 3:34 PM, vo.x wrote:

> Hello all,
> 
> Is there some best practice how to test Rails controller action which  
> is using send\_file method? Could you help me please?
> 
> Vit

On 23/02/2009, at 2:43 AM, vo.x wrote:

> end
> 
> Vit

Hi Vit. Just stub #send\_file as normal. For example:

before :each do  
controller.stub!(:send\_file).and\_return …  
end

it “should send the requested file” do  
controller.should\_receive(:send\_file).with(…).and\_return …  
do\_get  
end

Cheers,  
Nick

---

<div class="post-metadata">

**Author:** ![vo.x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/45deac/32.png) [@vo.x](https://www.ruby-forum.com/u/vo.x)\
**Post date:** [February 24, 2009, 2:16pm UTC](https://www.ruby-forum.com/t/send-file-testing/161049/4 "2009-02-24T14:16:39Z")

</div>

On 23 Ún, 18:41, Nick H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end  
> > controller.should\_receive(:send\_file).with(…).and\_return …  
> > do\_get  
> > end
> 
> Cheers,  
> Nick
> 
> * * *
> 
> rspec-users mailing list  
> removed\_email\_address@domain.invalid://rubyforge.org/mailman/listinfo/rspec-users

Hello Nick,

Thank you for your feedback, but your solution has one problem which  
actually led me to this question. If I will stub the send\_file method  
as you recommend, then later on is automatically called render method  
and I will get following error:

ActionView::MissingTemplate in ‘LogsController handling GET /logs/  
test.log/download should find the log requested’  
Missing template logs/download.erb in view path app/views

So there has to be set somewhere some flag, or provided some  
meaningful response … Im out of ideas 😕

Vit

---

<div class="post-metadata">

**Author:** ![vo.x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/45deac/32.png) [@vo.x](https://www.ruby-forum.com/u/vo.x)\
**Post date:** [February 24, 2009, 5:30pm UTC](https://www.ruby-forum.com/t/send-file-testing/161049/5 "2009-02-24T17:30:40Z")

</div>

On 24/02/2009, at 8:03 AM, vo.x wrote:

> > > found.")  
> > > end  
> > > removed\_email\_address@domain.invalid://rubyforge.org/mailman/listinfo/rspec-users  
> > > Missing template logs/download.erb in view path app/views
> 
> So there has to be set somewhere some flag, or provided some  
> meaningful response … Im out of ideas 😕
> 
> Vit

G’day Vit. ActionController is trying to render a view template for  
your “download” controller action because ActionController does that  
automatically. If you don’t want anything to be rendered, you need to  
tell ActionController that.  
-Nick

---

<div class="post-metadata">

**Author:** ![vo.x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/45deac/32.png) [@vo.x](https://www.ruby-forum.com/u/vo.x)\
**Post date:** [February 24, 2009, 11:41pm UTC](https://www.ruby-forum.com/t/send-file-testing/161049/6 "2009-02-24T23:41:06Z")

</div>

On 24 Ún, 17:26, Nick H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > which  
> > > > if log  
> > > > Hi Vit. Just stub #send\_file as normal. For example:  
> > > > Cheers,  
> > > > and I will get following error:  
> > > > G’day Vit. ActionController is trying to render a view template for  
> > > > your “download” controller action because ActionController does that  
> > > > automatically. If you don’t want anything to be rendered, you need to  
> > > > tell ActionController that.  
> > > > -Nick
> 
> * * *
> 
> rspec-users mailing list  
> removed\_email\_address@domain.invalid://rubyforge.org/mailman/listinfo/rspec-users

Well … and how to do that? Now its starting to be interesting 😉

Vit
