lundi 26 août 2019

Testing methods of class when instantiation creates HTTP request

I have some tests for an object that look like as follows:

let(:bot) { MyObject.new('/some/config/file.json') }

The problem is, the very act of instantiating MyObject creates an HTTP request. The following methods that I actually want to test do not create HTTP requests at all. E.g:

it "should parse Jira ticket key answers correctly" do                                                  
  expect(bot.offline_method(good_answer) == correct_outcome).to eq(true)
  expect(bot.offline_method(bad_answer) == correct_outcome).to eq(false)
end

How can I stub out the instantiation of MyObject.new itself?

For additional context, the MyObject.new instantiates a session using the google drive gem.

If I open an interactive Ruby environment and instantiate the bot as follows, it tells me the output is thus:

bot = MyObject.new('/some/config/file.json')
=> #<Object:0x007f6d07a10b50 @session=#<GoogleDrive::Session:0xabcdef1234567>, @MyObject_spreadsheet_responses_1=#<GoogleDrive::Worksheet spreadsheet_id="123456789abcdefghjijklmnopqrstuvwxyz", gid="123456789", title="title of first google sheet">, @MyObject_spreadsheet_responses_2=#<GoogleDrive::Worksheet spreadsheet_id="123456789abcdefghjijklmnopqrstuvwxyz", gid="1687481303", title="second_spreadsheet_title">>

So how exactly do I stub

let(:bot) { MyObject.new('/some/config/file.json') }

??

I know I want something like:

allow_any_instance_of(MyObject.new).to return {
#<Object:0x007f6d07a10b50 @session=#<GoogleDrive::Session:0xabcdef1234567>, @MyObject_spreadsheet_responses_1=#<GoogleDrive::Worksheet spreadsheet_id="123456789abcdefghjijklmnopqrstuvwxyz", gid="123456789", title="title of first google sheet">, @MyObject_spreadsheet_responses_2=#<GoogleDrive::Worksheet spreadsheet_id="123456789abcdefghjijklmnopqrstuvwxyz", gid="1687481303", title="second_spreadsheet_title">>
}

But I really have no clue what the exact syntax is I want or how to find it.

Thank you for any help!

Aucun commentaire:

Enregistrer un commentaire