如何使用 mocha 删除 send_file
最直接的尝试是这样做
@controller.stubs(:send_file)
,但这会导致输出错误,例如
ActionView::MissingTemplate: Missing template ...
So how do I stubaway the 2.3.x 系列的send_file
方法。
这个问题基本上与 ruby-forum February 2009 上提出的问题相同,这是从来没有真正回答过。
贵族
The most direct attempt is to do
@controller.stubs(:send_file)
But that results in an output error like
ActionView::MissingTemplate: Missing template ...
So how do I stub away the send_file
method from 2.3.x series.
The question is basically the same question that was asked on ruby-forum february 2009, which was never really answered.
Jarl
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 Rails 4(可能更早)[1],ImplicitRender 处理此问题,并检查“已执行?”。
所以,如果你想把它去掉,就去掉“已执行”?应该就足够了:
performed
的实现也不是那么难:因此,在您不希望或不能的情况下,存根
performed
只需确保response_body
不为零。[1] 在 5+ 中,此功能已移至 BasicImplicitRender 中。
With Rails 4 (and maybe earlier)[1], the ImplicitRender handles this, and it checks against "performed?".
So, if you want to stub it out, stubbing the "performed?" should suffice:
The implementation of
performed
is not that hard either:So, in cases where you don't want, or cannot, stub
performed
simply ensure theresponse_body
is not nil.[1] With 5+ this has been moved into BasicImplicitRender.
缺少模板错误可能表明 send_file 现在没有执行任何操作(因为存根),因此 Rails 将尝试沿着渲染链向下并尝试显示模板。
您现在已经有效地禁用了 send_file 调用,因此您需要更改存根以实际发送 Rails 可以解释的内容,并认为请求已得到处理,因为文件已提供,在这种情况下,不需要再渲染任何模板。
Missing template errors probably points to the fact that send_file is now not doing anything (because of the stub) so Rails will try to go down the rendering chain and try to display the template.
You've effectively disabled the send_file call now, so you will need to change your stub to actually sends something Rails can interpret and think the request is handled because the file is served, in which case no template will need to be rendered anymore.
我也不得不为此奋斗。据我所知,对我来说最好的解决方案是:(
控制器)
(测试)
我不喜欢更改代码以允许您进行测试,但我不太喜欢花费大量时间来进行测试为一个相当微不足道的问题找到正确的解决方案:)
I've had to fight with this as well. As far as I can tell, the best solution for me has been:
(controller)
(test)
I'm not a fan of altering your code to allow you to test, but I am less of a fan of spending hours and hours to find the right solution for a fairly trivial issue :)