RSpec send_file 测试

发布于 2024-10-12 01:41:33 字数 121 浏览 5 评论 0原文

如何测试发送文件的控制器操作?

如果我使用 controller.should_receive(:send_file) 执行此操作,测试会失败并显示“缺少模板”,因为没有任何内容被渲染。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

扛起拖把扫天下 2024-10-19 01:41:33

来自 谷歌搜索 around,似乎 render 也会在某个时刻被调用..但没有模板,会导致错误。

解决方案似乎也是将其存根:

controller.stub!(:render)

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)
凉风有信 2024-10-19 01:41:33

另一种有效的方法是:

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

对我来说,这抓住了一个事实,即 send_file 的预期副作用是安排不渲染任何其他内容。 (尽管如此,无可否认,让存根调用原始对象上的方法似乎有点奇怪。)

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

ゝ偶尔ゞ 2024-10-19 01:41:33

您还可以这样做:

result = get ....

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

You can also do this:

result = get ....

result.body.should eq IO.binread(path_to_file)
泅渡 2024-10-19 01:41:33

这对我在 RSpec 控制器测试中非常有用。我更喜欢不将其存根,而是调用原始文件,这样它就会返回文件,您甚至可以访问响应标头等...

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

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
老子叫无熙 2024-10-19 01:41:33

如果您正在制定请求规范(您应该 ),监视 send_file 是不可能的。

相反,您可以通过检查响应来满足自己:

expect(response).to have_http_status(:ok)
expect(response.body).to be_present

expect(response.headers["Content-Disposition"]).to eq(
  "attachment; filename=\"document.pdf\"; filename*=UTF-8''document.pdf",
)

如果您想断言内容与源文件匹配,则可以,但这通常是过度的,并且对于生成的内容,应在服务层上进行断言。

expect(response.body).to eq(record.file.download)

If you're doing request specs (you should), spying on send_file will not be possible.

Instead, satisfy yourself with inspecting the response:

expect(response).to have_http_status(:ok)
expect(response.body).to be_present

expect(response.headers["Content-Disposition"]).to eq(
  "attachment; filename=\"document.pdf\"; filename*=UTF-8''document.pdf",
)

If you want to assert that the contents match the source file, you can, but this is generally excessive, and for generated content the assertions should be made on a service layer.

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