Josh Hadik
Josh Hadik

Reputation: 433

Any way to test that a method starts pry?

I'm building a gem that allows for a much more convenient and configurable 'console' for ruby gem development than the current options (ie: 'bundle console').

As such, one of if not the most important aspects of the entire gem is that it does in fact open a console session, which I currently have set up in a start method:

class MyConsole 
  def start
    Pry.start(self)
  end
end

I am trying to test this functionality, but it's difficult because their's not a lot of good resources for this. It's also really annoying because every time I run this method in rspec, pry opens, and I have to exit it before finishing the rest of the tests.

I have three main questions:

  1. How can I run a method in rspec that starts pry without actually starting pry?
  2. How can I specifically test that that method does in fact start pry?
  3. Assuming 1 and 2 are possible, how can I test what context the method starts pry in? (notice how the start method calls Pry.start(self), meaning the pry session should open in the context of a MyConsole instance.)

Upvotes: 0

Views: 301

Answers (1)

Josh McMillan
Josh McMillan

Reputation: 734

The best way to do this would probably be using an RSpec spy.

Your test would probably look something like this:

describe MyConsole do
  describe '#start' do
    it 'calls Pry.start' do
      described_class.start
      expect(Pry).to have_received(:start).with('your_args_here')
    end
  end
end

This is a really good explanation of RSpec stubbing options, IMO, where you can learn more: https://about.futurelearn.com/blog/stubs-mocks-spies-rspec

Upvotes: 2

Related Questions