gpt4 book ai didi

ruby-on-rails-3 - 从 response_to format.csv 测试 rspec 中文件的内容

转载 作者:行者123 更新时间:2023-12-03 18:04:07 24 4
gpt4 key购买 nike

我的 Controller 中有以下代码可以导出 csv 文件

...
def export
@filename = 'users.csv'
@output_encoding = 'UTF-8'
@users = User.active_users #not the actual scope but this only returns active
respond_to do |format|
format.csv
end
end
...

我的规范中有以下内容
it "should only return active users"
get :export, :format => :csv
# i want to check that my mocked users_controller#export is only returning the active users and not the inactive ones
end
response.body当我检查它时,在这个测试中是空的。当在浏览器中点击此操作时,我将如何获取下载的规范中的 csv 文件,以便我可以检查结果?我在试图弄清楚这一点时遇到了一些障碍。

感谢您的任何帮助,您可以提供。

最佳答案

检查是否正在创建 CSV 文件的测试如下,假设 Controller 操作位于 'csv_create_path'

it 'should create a CSV file ' do
get csv_create_path
response.header['Content-Type'].should include 'text/csv'
end

关于ruby-on-rails-3 - 从 response_to format.csv 测试 rspec 中文件的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16741488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com