Search code examples
ruby-on-railsrubyrspecrspec-rails

How to access instance variables in rspec tests


How do I access pic in my rspec test? I keep getting the following error:

F

Failures:

  1) TrelloToAc::Card#pic return pic url from trello card
     Failure/Error: expect(result).to be_a String
       expected :pic to be a kind of String
     # ./spec/services/trello_to_ac/card_spec.rb:17:in `block (3 levels) in <top (required)>'

Finished in 1.97 seconds (files took 13.5 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/services/trello_to_ac/card_spec.rb:14 # TrelloToAc::Card#pic return pic url from trello card

It appears to be taking pic literally, like a string. Here is my code:

Test:

require 'rails_helper'

RSpec.describe TrelloToAc::Card do
  describe '#pic' do
    it 'return pic url from trello card' do
      result = @pic
      puts result
      expect(result).to be_a String
    end
  end
end

Code that is being tested:

class TrelloToAc::Card
  #
  #
  def pic
    @pic ||= card.attachments.last.url if card.attachments.any?
  end
  #
  #
end

Solution

  • This is how I ended up fixing this. I think this is more along the lines of what some of my commenters were saying. This also provides a little more info on the context of pic

    Test:

    require 'rails_helper'
    
    RSpec.describe TrelloToAc::Card do
    
      let(:trello_card) do
        cassette = 'trello_to_ac/cards/get'
        VCR.use_cassette(cassette, match_requests_on: [:method]) do
          Trello::Card.find 'itZmoIG4'
        end
      end
    
      let(:email_sent_label) do
        cassette = 'trello_to_ac/labels/get'
        VCR.use_cassette(cassette, match_requests_on: [:method]) do
          Trello::Label.find '60db99d5230c123a1ab808d0'
        end
      end
    
      let(:trello_to_ac_card) do
        TrelloToAc::Card.new(trello_card, 32188, 147, email_sent_label)
      end
      
      describe '#pic' do
        it 'returns pic url from trello card' do
          cassette = 'trello_to_ac/attachments/get'
          result = VCR.use_cassette(cassette, match_requests_on: [:method]) do
            trello_to_ac_card.pic
          end
          expect(result).to be_a String
        end
      end
    end
    

    Actual code:

    class TrelloToAc::Card
      attr_reader(
        :card, :order_number, :list_ac_campaign_id, :email_sent_label
      )
    
      def initialize(card, order_number, list_ac_campaign_id, email_sent_label)
        @card = card
        @order_number = order_number
        @list_ac_campaign_id = list_ac_campaign_id
        @email_sent_label = email_sent_label
      end
    
      def send_email_if_needed
        #
        #
        attach_trello_pic_to_ac_contact if pic
        #
        #
      end
    
      def pic
        @pic ||= card.attachments.last.url if card.attachments.any?
      end
    end
    

    This way I am testing an instance of the class, not instance variables. I have similar tests for other methods as well.