1

我有一个 rake 任务,它仅循环遍历我的所有产品并在产品保存时增加一个计数器。然后它打印出那个计数器。我如何测试什么号码呢puts

task(:resave_every_product => :environment) do
    total_products = 0
    counter = 0
    all_products = Product.find(:all)
    puts "done loading all products"
    puts "starting resaving..."
        all_products.each do |product|

            puts "current product is #{product.id}" if (counter%1000 == 0)
            counter += 1 if product.save
        end

    puts "done resaving products."
    puts "total products - #{all_products.size}"
    puts "products resaved - #{counter}"
end
4

1 回答 1

2

如果您愿意从使用 puts 切换到 Logger(除非您有使用 puts 的特定原因,否则这是可取的),您可以像这样在 RSpec 中测试输出:

 Rails.logger.should_receive(:info).with('done loading all products')
于 2013-06-10T19:16:17.890 回答