Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec send_file testing

How to test a controller action that sends a file?

If I do it with controller.should_receive(:send_file) test fails with "Missing template" because nothing gets rendered.

like image 859
Mirko Avatar asked Jan 15 '11 17:01

Mirko


2 Answers

From Googling around, it appears that render will also be called at some point .. but with no template, will cause an error.

The solution seems to be to stub it out as well:

controller.stub!(:render)
like image 75
Andy Lindeman Avatar answered Sep 21 '22 11:09

Andy Lindeman


Another way that works is:

controller.should_receive(:send_file).and_return{controller.render :nothing => true} 

To me, this captures the fact that the intended side effect of send_file is to arrange that nothing else be rendered. (Albeit, it admittedly seems a bit wonky to have the stub call a method on the original object.)

like image 30
ronen Avatar answered Sep 25 '22 11:09

ronen