简体   繁体   中英

How can I access the elements of a hash declare in a model with a model test in rails?

I have declared a model used solely to print a csv file. This model stores its values in a hash as such:

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

In ParentObject, I access the value of @data and use it to generate a csv file. I need to test this initialization method in an rspec test, but when I try to access @data, I receive a value of nil.

How can I test this initialization method?

The spec is

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

Use instance_variable_get()

You can refer this link for example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM