如何使用 mocha 删除 send_file

发布于 2024-11-29 22:43:29 字数 482 浏览 1 评论 0原文

最直接的尝试是这样做

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无可置疑 2024-12-06 22:43:29

对于 Rails 4(可能更早)[1],ImplicitRender 处理此问题,并检查“已执行?”。

所以,如果你想把它去掉,就去掉“已执行”?应该就足够了:

subject.stubs(:performed?).returns(true)
subject.expects(:send_file).
  with(image, disposition: "inline")

performed 的实现也不是那么难:

performed?
  response_body || (response && response.committed?)
end

因此,在您不希望或不能的情况下,存根 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:

subject.stubs(:performed?).returns(true)
subject.expects(:send_file).
  with(image, disposition: "inline")

The implementation of performed is not that hard either:

performed?
  response_body || (response && response.committed?)
end

So, in cases where you don't want, or cannot, stub performed simply ensure the response_body is not nil.


[1] With 5+ this has been moved into BasicImplicitRender.

孤独岁月 2024-12-06 22:43:29

缺少模板错误可能表明 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.

荒芜了季节 2024-12-06 22:43:29

我也不得不为此奋斗。据我所知,对我来说最好的解决方案是:(

控制器)

def some_action
  send_file(some_action_filename)
end

private

def some_action_filename
  "/public/something.tgz"
end

(测试)

test "some_action" do
  filename = Rails.root.join("tmp/testfile.tgz")
  assert FileUtils.touch filename  
  @controller.expects(:some_action_filename).at_least_once().returns(filename)
  post :some_action
end

我不喜欢更改代码以允许您进行测试,但我不太喜欢花费大量时间来进行测试为一个相当微不足道的问题找到正确的解决方案:)

I've had to fight with this as well. As far as I can tell, the best solution for me has been:

(controller)

def some_action
  send_file(some_action_filename)
end

private

def some_action_filename
  "/public/something.tgz"
end

(test)

test "some_action" do
  filename = Rails.root.join("tmp/testfile.tgz")
  assert FileUtils.touch filename  
  @controller.expects(:some_action_filename).at_least_once().returns(filename)
  post :some_action
end

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文