UserMailerTest ArgumentError:参数个数错误(0表示1)

时间:2016-01-11 17:44:51

标签: ruby-on-rails

我正在研究ruby on rails教程的第10章,并且收到了以下错误:

UserMailerTest#test_account_activation:
ArgumentError: wrong number of arguments (0 for 1)
app/mailers/user_mailer.rb:3:in `account_activation'
test/mailers/user_mailer_test.rb:6:in `block in <class:UserMailerTest>'

由于代码与教程中的代码相同,我不确定导致错误的原因。有什么想法吗?

user_mailer.rb

class UserMailer < ApplicationMailer
def account_activation(user)
  @user = user
  mail to: user.email, subject: "Account activation"
end

def password_reset
  @greeting = "Hi"
  mail to: "to@example.org"
  end
end

application_mailer.rb

class ApplicationMailer < ActionMailer::Base
  default from: "from@example.com"
  layout 'mailer'
end

routes.rb

Rails.application.routes.draw do

get 'sessions/new'

root 'welcome#index'

get 'contact' => 'welcome#contact'
get 'about' => 'welcome#about'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'

resources :users
resources :account_activations, only: [:edit]
match ':controller(/:action(/:id))', :via => :get
end

user_mailer_test.rb

require 'test_helper'

class UserMailerTest < ActionMailer::TestCase
test "account_activation" do
  mail = UserMailer.account_activation
  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

test "password_reset" do
  mail = UserMailer.password_reset
  assert_equal "Password reset", mail.subject
  assert_equal ["to@example.org"], mail.to
  assert_equal ["from@example.com"], mail.from
  assert_match "Hi", mail.body.encoded
end

end

谢谢!

1 个答案:

答案 0 :(得分:0)

在您的测试中,您正在调用UserMailer.account_activation,但该方法需要一个用户参数,因此您需要将其传递一个。