我有以下后台工作写入csv文件并通过电子邮件发送出去.我正在使用Tempfile类,因此在将其通过电子邮件发送给用户后,该文件将被删除.目前,当我查看csv文件时,我生成的结果如下所示:
["Client Application" "Final Price" "Tax" "Credit" "Base Price" "Billed At" "Order Guid" "Method of Payment Guid" "Method of Payment Type"]
["web" nil nil nil nil nil nil "k32k313k1j3" "credit card"]
Run Code Online (Sandbox Code Playgroud)
请忽略这些数据,但问题是,它是以ruby格式直接写入文件而不是删除""和[]字符.
请参阅以下代码:
class ReportJob
@queue = :report_job
def self.perform(client_app_id, current_user_id)
user = User.find(current_user_id)
client_application = Application.find(client_app_id)
transactions = client_application.transactions
file = Tempfile.open(["#{Rails.root}/tmp/", ".csv"]) do |csv|
begin
csv << ["Application", "Price", "Tax", "Credit", "Base Price", "Billed At", "Order ID", "Payment ID", "Payment Type"]
transactions.each do |transaction|
csv << "\n"
csv …Run Code Online (Sandbox Code Playgroud) 我试图在Ruby的StringIO的帮助下模拟 文件读取.以下是我的测试,接下来就是我在主类中的方法.
def test_get_symbols_from_StringIO_file
s = StringIO.new("YHOO,141414")
assert_equal(["YHOO,141414"], s.readlines)
end
def get_symbols_from_file (file_name)
IO.readlines(file_name, ',')
end
Run Code Online (Sandbox Code Playgroud)
我想知道这是否是我们模拟文件读取的方式,而且我想知道是否有其他方法来模拟类中的方法而不是使assert与内容相等.