【发布时间】: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