【问题标题】:RSpec send_file testing with check of Content-TypeRSpec send_file 测试并检查 Content-Type
【发布时间】:2012-02-26 17:55:13
【问题描述】:

我有一个 Rails 控制器,它发送具有不同 Content-Type 的文件

Exel-File 示例,控制器设置 Content-Type = "application/excel"

这是 RSpec 测试:

describe "GET getfile" do
  it "Excel File" do
    controller.stub(:render)
    controller.should_receive(:send_file)
    get :getfile, :name => 'test+xls'
    controller.response.header.should == '???'
  end
end

测试的答案是:

1) ExportController GET getfile Excel File
   Failure/Error: controller.response.header.should == ''
     expected: ""
          got: {"Content-Type"=>"text/html; charset=utf-8"} (using ==)
     Diff:
     @@ -1,2 +1,2 @@
     -""
     +"Content-Type" => "text/html; charset=utf-8"

【问题讨论】:

  • controller.stub(:render), controller.should_receive(:send_file)。这些行将控制器方法更改为在调用时返回 nil。您无法在使用 rspec 匹配器的单个测试中检查该方法是否被调用以及它的返回值。拆分测试或使用其他一些 gem 来获得期望。如果它没有帮助,也很高兴看到控制器代码。

标签: ruby-on-rails rspec sendfile


【解决方案1】:

如果您愿意(或已经使用)thoughtbot 应该是宝石,可以将回形针钩入其中

https://github.com/thoughtbot/paperclip/blob/master/shoulda_macros/paperclip.rb

并且有s3上传方式。

【讨论】:

    猜你喜欢
    • 2011-06-09
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    相关资源
    最近更新 更多