我正在使用设计中的request_confirmation_test.rb文件
我也包含了帮助程序,但文件在每一行显示错误。看起来缺少文件正常工作所必需的东西。这是我目前得到的错误
ConfirmationInstructionsTest#test_set_up_sender_from_custom_mailer_defaults:
NoMethodError: undefined method `from' for nil:NilClass
test/integration/request_confirmation_test.rb:46:in `block in <class:ConfirmationInstructionsTest>'
这是我测试文件的代码
require 'test_helper'
class ConfirmationInstructionsTest < ActionMailer::TestCase
def setup
setup_mailer
Devise.mailer = 'Devise::Mailer'
Devise.mailer_sender = 'johnmax.rtc@gmail.com'
end
def teardown
Devise.mailer = 'Devise::Mailer'
Devise.mailer_sender = 'please-change-me@config-initializers-devise.com'
end
def user
@user ||= create_user
end
def mail
@mail ||= begin
user
ActionMailer::Base.deliveries.first
end
end
test 'email sent after creating the user' do
assert_not_nil mail
end
# test 'content type should be set to html' do
# assert mail.content_type.include?('text/html')
# end
test 'send confirmation instructions to the user email' do
mail
assert_equal [user.email], mail.to
end
test 'set up sender from configuration' do
assert_equal ['johnmax.rtc@gmail.com'], mail.from
end
test 'set up sender from custom mailer defaults' do
Devise.mailer = 'Users::Mailer'
assert_equal ['johnmax.rtc@gmail.com'], mail.from
end
# test 'set up sender from custom mailer defaults with proc' do
# Devise.mailer = 'Users::FromProcMailer'
# assert_equal ['johnmax.rtc@gmail.com'], mail.from
# end
# test 'custom mailer renders parent mailer template' do
# Devise.mailer = 'Users::Mailer'
# assert_present mail.body.encoded
# end
# test 'set up reply to as copy from sender' do
# assert_equal ['johnmax.rtc@gmail.com'], mail.reply_to
# end
# test 'set up reply to as different if set in defaults' do
# Devise.mailer = 'Users::ReplyToMailer'
# assert_equal ['johnmax.rtc@gmail.com'], mail.from
# assert_equal ['johnmax.rtc@gmail.com'], mail.reply_to
# end
test 'set up subject from I18n' do
store_translations :en, devise: { mailer: { confirmation_instructions: { subject: 'Account Confirmation' } } } do
assert_equal 'Account Confirmation', mail.subject
end
end
test 'subject namespaced by model' do
store_translations :en, devise: { mailer: { confirmation_instructions: { user_subject: 'User Account Confirmation' } } } do
assert_equal 'User Account Confirmation', mail.subject
end
end
test 'body should have user info' do
assert_match user.email, mail.body.encoded
end
test 'body should have link to confirm the account' do
host, port = ActionMailer::Base.default_url_options.values_at :host, :port
if mail.body.encoded =~ %r{<a href=\"http://#{host}:#{port}/users/confirmation\?confirmation_token=([^"]+)">}
assert_equal $1, user.confirmation_token
else
flunk "expected confirmation url regex to match"
end
end
# test 'renders a scoped if scoped_views is set to true' do
# swap Devise, scoped_views: true do
# assert_equal user.email, mail.body.decoded
# end
# end
# test 'renders a scoped if scoped_views is set in the mailer class' do
# begin
# Devise::Mailer.scoped_views = true
# assert_equal user.email, mail.body.decoded
# ensure
# Devise::Mailer.send :remove_instance_variable, :@scoped_views
# end
# end
test 'mailer sender accepts a proc' do
swap Devise, mailer_sender: proc { "another@example.com" } do
assert_equal ['another@example.com'], mail.from
end
end
end
这是帮助文件
require 'active_support/test_case'
class ActiveSupport::TestCase
VALID_AUTHENTICATION_TOKEN = 'AbCdEfGhIjKlMnOpQrSt'.freeze
def setup_mailer
ActionMailer::Base.deliveries = []
end
def store_translations(locale, translations, &block)
# Calling 'available_locales' before storing the translations to ensure
# that the I18n backend will be initialized before we store our custom
# translations, so they will always override the translations for the
# YML file.
I18n.available_locales
I18n.backend.store_translations(locale, translations)
yield
ensure
I18n.reload!
end
def generate_unique_email
@@email_count ||= 0
@@email_count += 1
"test#{@@email_count}@example.com"
end
def valid_attributes(attributes={})
{ first_name: "usertest",
email: generate_unique_email,
password: '12345678',
password_confirmation: '12345678' }.update(attributes)
end
def new_user(attributes={})
User.new(valid_attributes(attributes))
end
def create_user(attributes={})
User.create!(valid_attributes(attributes))
end
def create_admin(attributes={})
valid_attributes = valid_attributes(attributes)
valid_attributes.delete(:first_name)
Admin.create!(valid_attributes)
end
def create_user_without_email(attributes={})
UserWithoutEmail.create!(valid_attributes(attributes))
end
def create_user_with_validations(attributes={})
UserWithValidations.create!(valid_attributes(attributes))
end
# Execute the block setting the given values and restoring old values after
# the block is executed.
def swap(object, new_values)
old_values = {}
new_values.each do |key, value|
old_values[key] = object.send key
object.send :"#{key}=", value
end
clear_cached_variables(new_values)
yield
ensure
clear_cached_variables(new_values)
old_values.each do |key, value|
object.send :"#{key}=", value
end
end
def clear_cached_variables(options)
if options.key?(:case_insensitive_keys) || options.key?(:strip_whitespace_keys)
Devise.mappings.each do |_, mapping|
mapping.to.instance_variable_set(:@devise_parameter_filter, nil)
end
end
end
end
任何帮助都将受到高度赞赏。