我在Ruby on Rails应用程序中遇到2个错误。我正在尝试从Rails教程中完成示例应用程序。
以下是我的错误:
1) Error:
UserMailerTest#test_account_activation:
ArgumentError: wrong number of arguments (1 for 0) app/mailers/user_mailer.rb:3:in `account_activation' test/mailers/user_mailer_test.rb:9:in `block in <class:UserMailerTest>'
2) Failure:
UserMailerTest#test_password_reset [/home/andy/Ruby_Projects/sam
ple_app/test/mailers/user_mailer_test.rb:21]: Expected: ["from@example.com"] Actual: ["noreply@example.com"]
这是我的两个问题的代码:
require 'test_helper'
class UserMailerTest < ActionMailer::TestCase
test "account_activation" do
user = users(:andy)
user.activation_token = User.new_token
mail = UserMailer.account_activation(user)
assert_equal "Account activation", mail.subject
assert_equal [user.email], mail.to
assert_equal ["noreply@example.com"], mail.from
assert_match user.name, mail.body.encoded
assert_match user.activation_token, mail.body.encoded
assert_match CGI::escape(user.email), 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
这是UserMailer.rb:
class UserMailer < ApplicationMailer
def account_activation
user = user
mail to: user.email, subject: "Account activation"
end
def password_reset
@greeting = "Hi"
mail to: "to@example.org"
end
end
我一直在跟着教程检查我的代码与教程中的内容,但仍然无法找到我做错了什么。如果您需要更多信息,请与我们联系。我会尝试提供它。
答案 0 :(得分:0)
您没有为account_activation
方法定义参数。您需要添加user
参数:
def account_activation(user)
@user = user
mail to: user.email, subject: "Account activation"
end