简体   繁体   English

RSpec。 如何使用嵌套的私有方法(模拟/存根)测试方法?

[英]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"? 同志们,如何用嵌套的私有方法“ self.merge_op”测试方法“ self.default”? I m noob in RSpec. What is more suitable for such case: mocking/stubbing? Functionality of file - it m noob in RSpec. What is more suitable for such case: mocking/stubbing? Functionality of file - it m noob in RSpec. What is more suitable for such case: mocking/stubbing? Functionality of file - it m noob in RSpec. What is more suitable for such case: mocking/stubbing? Functionality of file - it s some kind of MailClient m noob in RSpec. What is more suitable for such case: mocking/stubbing? Functionality of file - it是某种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

I would just test the default method without regards to the fact that it uses a private method to do its work. 我只是测试default方法,而不考虑它使用私有方法来完成工作的事实。 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. 您可以更改default方法的内部工作方式,甚至可以取消私有方法,而无需更改测试。

The only thing I would think about mocking is whatever the settings object is in the private merge_op method. 我想考虑的唯一事情就是私有merge_op方法中的settings对象。 That object is effectively an input to the default method. 该对象实际上是default方法的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM