Forum: RSpec send_file testing

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
vo.x (Guest)
on 2009-02-22 22:59
(Received via mailing list)
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
vo.x (Guest)
on 2009-02-23 09:44
(Received via mailing list)
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
Nick H. (Guest)
on 2009-02-23 20:17
(Received via mailing list)
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
vo.x (Guest)
on 2009-02-24 15:16
(Received via mailing list)
On 23 Ún, 18:41, Nick H. <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
Nick H. (Guest)
on 2009-02-24 18:30
(Received via mailing list)
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
vo.x (Guest)
on 2009-02-25 00:41
(Received via mailing list)
On 24 Ún, 17:26, Nick H. <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
This topic is locked and can not be replied to.