通过检查Content-Type进行RSpec send_file测试

rom*_*mus 5 rspec ruby-on-rails sendfile

我有一个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
Run Code Online (Sandbox Code Playgroud)

测试的答案是:

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"
Run Code Online (Sandbox Code Playgroud)

Joh*_*ter 0

如果您愿意(或已经使用)thoughtbot 应该是一个宝石,回形针可以钩住它

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

并且有s3上传方法。