为什么Rails实例方法可以用作rspec中的类方法 [英] Why Rails instance method can be used as class method in rspec
问题描述
我在一篇有关在Rails应用程序中发送邮件的文章中找到了一个片段:
I found a snippet in an article about sending mails in a Rails application:
class ExampleMailerPreview < ActionMailer::Preview
def sample_mail_preview
ExampleMailer.sample_email(User.first)
end
end
在此链接中: http:// www.gotealeaf.com/blog/handling-emails-in-rails 。
我不知道为什么使用这种方法: sample_email ()
,在我看来应该是实例方法,可以像下面的类方法那样通过 ExampleMailer.sample_email()
进行访问。谁能解释?
I do not know why the method: sample_email()
, which in my mind should be an instance method, can be accessed like class method here as ExampleMailer.sample_email()
. Can anyone explain?
推荐答案
这不是rspec,而是ActionMailer。查看:
It's not an rspec thing, it's an ActionMailer thing. Looking at:
https://github.com/rails/rails/blob/master/actionmailer/lib/action_mailer/base.rb
看看第135-146行中的注释:
Take a look at the comments in lines 135-146:
# = Sending mail
#
# Once a mailer action and template are defined, you can deliver your message or defer its creation and
# delivery for later:
#
# NotifierMailer.welcome(User.first).deliver_now # sends the email
# mail = NotifierMailer.welcome(User.first) # => an ActionMailer::MessageDelivery object
# mail.deliver_now # generates and sends the email now
#
# The <tt>ActionMailer::MessageDelivery</tt> class is a wrapper around a delegate that will call
# your method to generate the mail. If you want direct access to delegator, or <tt>Mail::Message</tt>,
# you can call the <tt>message</tt> method on the <tt>ActionMailer::MessageDelivery</tt> object.
该功能是通过在ActionMailer :: Base类上定义一个method_missing方法来实现的,该方法类似于: / p>
The functionality is implemented by defining a method_missing method on the ActionMailer::Base class that looks like:
def method_missing(method_name, *args) # :nodoc:
if action_methods.include?(method_name.to_s)
MessageDelivery.new(self, method_name, *args)
else
super
end
end
本质上,在ActionMailer实例上定义一个方法(在注释示例中为NotifierMailer),然后在类上对其进行调用,将创建一个新的MessageDelivery实例,该实例将委派给ActionMailer的新实例课。
Essentially, defining a method on an ActionMailer instance (NotifierMailer in the comment example) and then calling it on the class creates a new MessageDelivery instance which delegates to a new instance of the ActionMailer class.
这篇关于为什么Rails实例方法可以用作rspec中的类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!