2012-02-04 73 views
4

我有一個Rails控制器,可發送一個文件具有不同的內容類型RSpec的由send_file與內容類型檢查的測試

示例了Exel-文件時,控制器設置的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" 
+0

'controller.stub(:render)','controller.should_receive(:send_file)'。 這些行更改控制器方法以在調用時返回nil。您無法檢查該方法是否被調用,並且它是使用rspec匹配器在單個測試中返回的值。拆分測試或使用其他寶石的期望。 如果沒有幫助,看到控制器代碼太好了。 – faron 2013-12-02 09:23:12

回答