开发者

RSpec send_file testing

开发者 https://www.devze.com 2023-02-04 04:40 出处:网络
How to test a controller action tha开发者_JS百科t sends a file? If I do it with controller.should_receive(:send_file) test fails with \"Missing template\" because nothing gets rendered.From Googling

How to test a controller action tha开发者_JS百科t sends a file?

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


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)


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.)


You can also do this:

result = get ....

result.body.should eq IO.binread(path_to_file)


This has worked great for me in RSpec controller tests. I prefer to not stub it out, but instead call the original so it does return the file and you can even access the response headers, etc...

expect(controller).to receive(:send_file).with(some_filepath, type: "image/jpg").and_call_original
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号