2
votes

I want to make a spec for the mailing that is already implemented and I have this:

  • rspec-rails 3.5.2
  • rails 5
  • ruby 2.3.1

app/mailers/application_mailer.rb

class ApplicationMailer < ActionMailer::Base
  default from: '[email protected]'
  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

When I run

rspec spec/mailers/usermailer_spec.rb

I receive this error message on the terminal:

[...]spec/mailers/usermailer_spec.rb:4:in `': uninitialized constant UserMailer (NameError) [...]

1
Not sure, but you have usermailer_spec.rb & user_mailer.rb (don't match) Try maybe renaming spec to user_mailer_spec.rb and see if this helps - Tomasz Giba
Thank you for your suggestion and unfortunately it didn't work to me but I'll let it to be user_mailer_spec.rb like you sad. - Fillype Farias
Have you ensured that the Mailer files become initialized while loading the test? Also you might wanna try instantiating thr Mailer class in your rails console. If that does not work you should check your path settings - A. Neumann

1 Answers

2
votes

You need to require require 'rails_helper' instead of spec_helper on the spec file.