[英]How to access a hash defined in a model in a test fixture file in rails
[英]How can I access the elements of a hash declare in a model with a model test in rails?
我聲明了僅用於打印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
使用instance_variable_get()
您可以參考此鏈接的示例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.