所以,我正在经历同样的事情。我试图在不加载所有 Rails 的情况下测试我的邮件。
最终解决我的问题的是将其添加到我的测试中:(请注意,我的测试位于 test/unit/mailers/my_mailer_test.rb - 您可能需要调整路径)
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.view_paths = File.expand_path('../../../../app/views', __FILE__)
基本上,如果没有指向您的视图目录的视图路径,则找不到模板并且所有部分(html、文本等)都是空白的。
注意:指定的目录不是实际模板所在的目录。邮件程序知道在以类本身命名的模板根目录中查找目录。
这是 minitest/spec 中的示例
require 'minitest/spec'
require 'minitest/autorun'
require "minitest-matchers"
require 'action_mailer'
require "email_spec"
# NECESSARY TO RECOGNIZE HAML TEMPLATES
unless Object.const_defined? 'Rails'
require 'active_support/string_inquirer'
class Rails
def self.env
ActiveSupport::StringInquirer.new(ENV['RAILS_ENV'] || 'test')
end
end
require 'haml/util'
require "haml/template"
end
# END HAML SUPPORT STUFF
require File.expand_path('../../../../app/mailers/my_mailer', __FILE__)
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.view_paths = File.expand_path('../../../../app/views', __FILE__)
describe MyMailer do
include EmailSpec::Helpers
include EmailSpec::Matchers
let(:the_email){ MyMailer.some_mail() }
it "has the right bit of text" do
the_email.must have_body_text("some bit of text")
end
end