Serg Ra6n
Serg Ra6n

Reputation: 93

RSpec. How to test method with nested private method (mocking/stubbing)?

Comrades, how to test method "self.default" with nested private method "self.merge_op"? Im noob in RSpec. What is more suitable for such case: mocking/stubbing? Functionality of file - its some kind of MailClient

class MailClient
  def self.default
    options = self.merge_op
    @clients[options] = MailClient.send :new unless @clients.has_key?(options)
    @clients[options]
  end

def self.merge_op(opts={:smtp => {}, :pop3 => {}})
def_smtp_opts = {:address => settings.mail_smtp_server,
                 :port => settings.mail_smtp_port,
                 :domain => settings.mail_smtp_domain,
                 :user_name => settings.mail_smtp_user_name,
                 :password => settings.mail_smtp_user_pass,
                 :authentication => 'plain',
                 :enable_starttls_auto => true}

end

Upvotes: 0

Views: 232

Answers (1)

dpassage
dpassage

Reputation: 5453

I would just test the default method without regards to the fact that it uses a private method to do its work. Figure out what the expected outputs for given inputs are.

The primary reason I say this is that it makes your tests more robust. You can change the internal workings of the default method, maybe even eliminating the private method, without changing the test.

The only thing I would think about mocking is whatever the settings object is in the private merge_op method. That object is effectively an input to the default method.

Upvotes: 1

Related Questions