我要求覆盖ActionMailer的邮件方法.这个方法受到保护所以在子类中,我还将mail方法定义为protected:
protected def mail(headers={},&block) #add a check to avoid sending emails that end with ".old" unless headers[:to] =~ /\.old$/ super(headers,&block) end end
这样,当外发电子邮件地址以.old结尾时,该方法应返回nil而不发送电子邮件.
但是,在我的单元测试中,似乎电子邮件仍然是ActionMailer :: Base.deliveries
这是我的单元测试:
describe 'BaseNotifier' do class TestNotifier < BaseNotifier def mailer(to,from,subject) mail(:to => to,:from => from,:subject => subject) end end before(:each) do ActionMailer::Base.delivery_method = :test ActionMailer::Base.perform_deliveries = true ActionMailer::Base.deliveries.clear end it "should not send emails to a user with an email that ends in '.old'" do TestNotifier.mailer("some_email@gmail.com.old","from@gmail.com","test email").deliver puts "what do we have here " + ActionMailer::Base.deliveries.first.to_s ActionMailer::Base.deliveries.length.should == 0 end end
我创建了一个测试类,并为重写邮件的类创建子类(这是它在系统中的使用方式).然后我发一封电子邮件并调用.deliver方法.
更新:我认为交付方法不是问题.当我这样做:放入TestNotifier.mailer(“some_email@gmail.com.old”,“from @gmail.com”,“测试电子邮件”)我收到如下相同的电子邮件.我也尝试以这种方式覆盖mail方法:
def mail(headers={},&block) else super({:to=>nil,:from=>nil,:boundary =>nil,:date => nil,:message =>nil,:subject =>nil,:mime_version => nil,:content_type => nil,:charset =>nil,:content_transfer_encoding => nil},&block) end end
这给了我一个未定义的方法失败’ascii_only?’对于nil:NilClass在这一行:TestNotifier.mailer(“some_email@gmail.com.old”,“测试电子邮件”).
#create一个覆盖传递方法的类,什么都不做.
class NonSendingEmail; def交付;结束;结束
def mail(headers = {},& block)
#add检查以避免发送以“.old”结尾的电子邮件
除非标题[:to] =〜/ .old $/
super(标题和&块)
其他
NonSendingEmail.new
结束
结束
但我仍然有相同的结果.
似乎传递方法正在生成一个空的电子邮件?我想做的是什么都没有产生.这是来自deliveryies数组的电子邮件:
Date: Thu,19 Jan 2012 00:00:00 +0000 Message-ID: <4f18344f94d0_12267803589ac2731d@Ramys-MacBook-Pro.local.mail> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit