0

I'm currently trying to write a test for this method:

#fetch message from api

def message_instance
    project_id = ENV['SIGNALWIRE_PROJECT_KEY']
    project_tkn = ENV['SIGNALWIRE_TOKEN']
    host_url = ENV['SIGNALWIRE_HOST']
@client = Signalwire::REST::Client.new project_id, project_tkn, signalwire_space_url: host_url
    message = @client.messages(sid).fetch
end

and am using FactoryBot to simulate a received message @message = build :message, status: :received

But I can't get my head around how to test every line of the method. Hoping someone can break down how I can properly stub a request for this?

Edit: I so far I've tried this:

  describe 'message_instance' do
      it 'returns message instance' do
        
        allow(ENV).to receive(:[]).with('SIGNALWIRE_PROJECT_KEY').and_return('AC123')
        @message = build :message, status: 'received', sid: '123456789'
       
       
        @message.message_instance.should eq @client
        
      end
    end

which returns this error:

 "BUNDLER_ORIG_RUBYLIB"=>"BUNDLER_ENVIRONMENT_PRESERVER_INTENTIONALLY_NIL", "BUNDLER_ORIG_RUBYOPT"=>"BUNDLER_ENVIRONMENT_PRESERVER_INTENTIONALLY_NIL", "BUNDLE_GEMFILE"=>"/vagrant/oyetext-backend/Gemfile", "BUNDLE_BIN_PATH"=>"/usr/share/rvm/rubies/ruby-2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/libexec/bundle", "BUNDLER_VERSION"=>"2.1.4", "RUBYOPT"=>"-r/usr/share/rvm/rubies/ruby-2.7.2/lib/ruby/2.7.0/bundler/setup", "RUBYLIB"=>"", "RAILS_ENV"=>"test", "SIGNALWIRE_PROJECT_KEY"=>"test", "SIGNALWIRE_TOKEN"=>"test", "SIGNALWIRE_NUMBER"=>"+19999999999"} received :[] with unexpected arguments
         expected: ("SIGNALWIRE_PROJECT_KEY")
              got: ("SIGNALWIRE_TOKEN")
        Please stub a default value first if message might be received with other args as well. 
Ruby Newb
  • 53
  • 5

1 Answers1

0

I don't really think stubbing ENV is a good idea, as you could see that is used for way more stuff than your code logic, like bundler or ruby itself.
Instead of calling allow(ENV).to..., I'd try just with:

ENV['SIGNALWIRE_PROJECT_KEY'] = 'AC123'
Alter Lagos
  • 12,090
  • 1
  • 70
  • 92