【问题标题】:rspec don't recognize mailing classrspec 不识别邮件类
【发布时间】:2017-03-17 12:44:48
【问题描述】:

我想为已经实现的邮件制定一个规范,我有这个:

  • rspec-rails 3.5.2
  • 导轨 5
  • 红宝石 2.3.1

app/mailers/application_mailer.rb

class ApplicationMailer < ActionMailer::Base
  default from: 'aaa@gmail.com'
  layout 'mailer'
end

app/mailers/user_mailer.rb

class UserMailer < ApplicationMailer
  def welcome(user)
    @user = user
    mail(to: @user.email, subject: 'Welcome!')
  end
end

config/environments/test.rb

Rails.application.configure do
  #[...]
  config.action_mailer.delivery_method = :test
  #[...]
end

spec/mailers/usermailer_spec.rb

require 'spec_helper'

RSpec.describe UserMailer, :type => :mailer do
 #[...]
end

当我跑步时

rspec 规范/mailers/usermailer_spec.rb

我在终端上收到此错误消息:

[...]spec/mailers/usermailer_spec.rb:4:in `': 未初始化的常量 UserMailer (NameError) [...]

【问题讨论】:

  • 不确定,但您有 usermailer_spec.rbuser_mailer.rb(不匹配)尝试将规范重命名为 user_mailer_spec.rb 看看是否有帮助
  • 感谢您的建议,不幸的是它对我没有用,但我会让它成为 user_mailer_spec.rb 像你一样伤心。
  • 您是否确保 Mailer 文件在加载测试时被初始化?此外,您可能想尝试在您的 rails 控制台中实例化 thr Mailer 类。如果这不起作用,您应该检查您的路径设置

标签: ruby-on-rails rspec


【解决方案1】:

您需要在规范文件中要求 require 'rails_helper' 而不是 spec_helper

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    相关资源
    最近更新 更多