简体   繁体   中英

How to fix ArgumentError: wrong number of arguments (given 0, expected 1) - RoR

I'm beginner and in the mailer code Ruby on Rails I have an error "ArgumentError: wrong number of arguments (given 0, expected 1)" between my code and his test.

Error:

UtilisateurMailerTest#test_account_activation:

ArgumentError: wrong number of arguments (given 0, expected 1)
    app/mailers/utilisateur_mailer.rb:8:in `account_activation'
    test/mailers/utilisateur_mailer_test.rb:6 

utilisateur_mailer_test.rb:

class UtilisateurMailerTest < ActionMailer::TestCase
  test "account_activation" do
           mail = UtilisateurMailer.account_activation
(line6)    assert_equal "Account activation", mail.subject
           assert_equal ["to@example.org"], mail.to
           assert_equal ["from@example.com"], mail.from
           assert_match "Hi", mail.body.encoded
  end

utilisateur_mailer.rb: ........

Line8:
  def account_activation(utilisateur)
    @utilisateur = utilisateur
    mail to: utilisateur.email, subject: "Activation du compte"
  end

I've tryed to delete ", mail.subject" and the subject: "Activation du compte" and it returns: ArgumentError: wrong number of arguments (given 1, expected 2..3)

I'm a novice.. thanks for your answers!

has you can see the error message is telling you that one of your method is waiting for an argument but nothing was passed to it.

In your utilisateur_mailer_test.rb , you can see that you are calling the method account_activation that you created in the utilisateur_mailer.rb .

As you can see this method needs an utilisateur parameter to be executed but when you are calling this method inside of your utilisateur_mailer_test.rb , you are not passing anything to it: mail = UtilisateurMailer.account_activation

You must specify an utilisateur argument here by creating a user through a factory using FactoryBot with Faker for example.

Good luck: :)

I faced this issue while upgrading from Rails v5.2.3 to v5.2.4 and fixed it by upgrading the following gem

gem 'awesome_nested_set', '~> 3.2.0'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM