我已经声明了一个仅用于打印 csv 文件的模型。该模型将其值存储在哈希中,如下所示:
class MyObject < ParentObject
def initialize(args = {})
super(args)
@campaign = args[:symbol]
@campaign.report.data[:donations][:tiers].each do |tier|
@data = []
tmp_arr = []
@data << ['Date','User Name','Email']
tier.values.sort{|a,b| a.name <=> b.name}.each do |c|
tmp_arr = []
tmp_arr << c.created_at
tmp_arr << c.user.name
tmp_arr << c.user.email
@data << tmp_arr
end
end
end
end
在 ParentObject 中,我访问 @data 的值并使用它来生成一个 csv 文件。我需要在 rspec 测试中测试这个初始化方法,但是当我尝试访问 @data 时,我收到一个 nil 值。
如何测试这种初始化方法?
规格是
describe Object do
it "should store donor information" do
@user = FactoryGirl.build(:user)
@campaign = FactoryGirl.build(:current_campaign)
@contribution_tier = FactoryGirl.build(:contribution_tier, campaign: @campaign)
@contribution = FactoryGirl.build(:contribution, campaign: @campaign, user: @user)
@donor = Reports::Donors.new(campaign: @campaign)
puts @data.first
end