我创建了这个模块:support / mailer_macros.rb
module MailerMacros
def last_email
ActionMailer::Base.deliveries.last
end
def reset_email
ActionMailer::Base.deliveries = []
end
end
我想从testhelper访问它,所以我在test / test_helper.rb中执行了此操作:
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
#require "capybara/rails"
require "minitest/rails/capybara"
require "support/mailer_macros"
class ActiveSupport::TestCase
include MailerMacros
reset_email
end
但是当我运行测试时,我收到错误:
undefined local variable or method `reset_email' for ActiveSupport::TestCase:Class
怎么了?谢谢!
答案 0 :(得分:2)
包含模块意味着模块的方法可用作实例方法,而不是类方法。您正尝试在班级级别运行reset_email
。
要解决此问题,extend MailerMacros
而不是包含它:
class ActiveSupport::TestCase
extend MailerMacros
reset_email
end