1

我创建了这个模块: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

怎么了?谢谢!

4

1 回答 1

2

包含一个模块意味着该模块的方法可用作实例方法,而不是类方法。您正在尝试reset_email在班级级别上运行。

要解决此问题,extend MailerMacros而不是包含它:

class ActiveSupport::TestCase

  extend MailerMacros
  reset_email

end
于 2013-10-24T09:59:03.147 回答