dimanche 22 octobre 2017

RSpec 3.6 and Rails 5.0.1 - ArgumentError: wrong number of arguments (given 0, expected 1)

I'm trying to write a mailer test using RSpec. However, it is failing and I cannot figure out why. My failure notification is the following:

Failures:

  1) UserMailer welcome_email sends an email upon new user registration
     Failure/Error:
       def welcome_email(id)
         @user = User.find(id)
         mail(to: @user.email, subject: 'Welcome on board!')
       end

     ArgumentError:
       wrong number of arguments (given 0, expected 1)
     # ./app/mailers/user_mailer.rb:4:in `welcome_email'
     # ./spec/mailers/user_mailer_spec.rb:10:in `block (3 levels) in <top (required)>'

The relevant code in the file I'm testing is the following:

class UserMailer < ApplicationMailer
  default from: 'noreply@example.com'

  def welcome_email(id)
    @user = User.find(id)
    mail(to: @user.email, subject: 'Welcome on board!')
  end
end

And this is my spec:

require "rails_helper"


RSpec.describe UserMailer, :type => :mailer do
  describe "welcome_email" do
    let(:mail) { UserMailer.welcome_email }

    it 'sends an email upon new user registration' do
      expect(mail.subject).to eq('Welcome on board!')
    end   
  end
end

I have tried all suggestions for this error I could find elsewhere to no avail. What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire