Search code examples
ruby-on-railsrakeactionmailer

ActionMailer testing ArgumentError: wrong number of arguments (0 for 1)


Can't figure out why the bundle exec rake test command fails but when I manually try to send the email it works and displays as it should.

Error message: ArgumentError: wrong number of arguments (0 for 1) and it highlights def stock_alert_email(stock) from user_mailer.rb

user_mailer.rb

def stock_alert_email(stock)
    @stock = stock
    mail(to: 'email@email.org', subject: "Stock Alert! %s has reached %s" % [@stock[0].to_s, @stock[1].to_s])
end

user_mailer_test.rb

test "stock_alert_email" do
  email = UserMailer.stock_alert_email().deliver_now
  assert_not ActionMailer::Base.deliveries.empty?

  # Test the body of the sent email contains what we expect it to
  assert_equal ['email@email.org'], email.from
  assert_equal ['email@email.org'], email.to
  assert_equal "Stock Alert! %s has reached %s" % [@stock[0].to_s, @stock[1].to_s], email.subject
  assert_equal read_fixture('stock_alert_email').join, email.body.to_s
end

where I call the mail to send

UserMailer.stock_alert_email(@stock).deliver_now

When I use binding.pry before the above^^ line I see that @stock is

[1] pry(main)> @stock
=> ["AAPL", 114.21]

Solution

  • The problem is here in this line

    email = UserMailer.stock_alert_email().deliver_now
    

    You are not passing any argument to the stock_alert_email while it expects an argument.