2014-10-07 48 views
0

我的目标是运行由axlsx-rails gem生成的文件的规格,并使用自定义的rspec匹配器,我发现here在axslx生成的文件上运行规格

在我的规格例如,我有:

describe Statistic::CompanyTableSetsController do 
    login_user # this just for Devise Auth 
    it "generates a proper excel sheet" do 
    ... 
    get "show", format: :xlsx, id: id 
    response.content_type.to_s.should eq Mime::Type.lookup_by_extension(:xlsx).to_s 
    end 
end 

但是当我运行我的测试,我得到:

Failure/Error: response.content_type.to_s.should eq Mime::Type.lookup_by_extension(:xlsx).to_s 

    expected: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" 
     got: "text/html" 

当然,我不能在其他规格的例子在工作表...

认为我错过了这里的东西,因为在我的浏览器中,Excel工作表正常生成。

谢谢你的帮助。

回答

0

检查你的测试日志,如果你的请求是通过xlsx格式。你的测试应该工作正常。

get作为查询字符串参数追加format?也许你需要:

get "/company_table_sets/#{id}.xlsx" 

你也应该能够使用:

response.content_type.to_s.should eq Mime::XLSX.to_s