users_signup_test 和 user_test 在 ActionMailer 配置后均失败(基于 Michael Hartl 教程)

Both users_signup_test and user_test fail after ActionMailer configuration(based off of Michael Hartl tutorial)

我正在尝试制作一个简单的应用程序,它只向注册的任何人发送一封带有狗图片的电子邮件,以作为我自己在其他应用程序中创建邮件程序的示例。在我为 ActionMailer 添加代码之前,我的用户模型和集成测试已经通过。我不确定发生了什么 on/why 他们失败了,因为除了添加私人电子邮件之外我没有对 user.rb 模型或 users_controller.rb 文件进行任何更改(这不是问题,我已经删除了该方法并将模型恢复到其原始状态并得到相同的错误消息)。如果有人可以帮助我或知道为什么会发生这种情况,那就太好了!

编辑:这是我的 bitbucket 存储库:https://bitbucket.org/corourke24/daves-double-down-dogs

我收到的错误消息如下:

Failure:
UsersSignupTest#test_valid_signup_info [/home/ubuntu/workspace/test/integration/users_signup_test.rb:17]:
"User.count" didn't change by 1.
Expected: 4
  Actual: 3


bin/rails test test/integration/users_signup_test.rb:15

...F

Failure:
UserTest#test_email_validation_should_accept_valid_email_addresses [/home/ubuntu/workspace/test/models/user_test.rb:39]:
Expected false to be truthy.


bin/rails test test/models/user_test.rb:34

F

Failure:
UserTest#test_user_should_have_valid_email [/home/ubuntu/workspace/test/models/user_test.rb:14]:
Expected false to be truthy.


bin/rails test test/models/user_test.rb:13

F

Failure:
UserTest#test_user_should_be_valid [/home/ubuntu/workspace/test/models/user_test.rb:10]:
Expected false to be truthy.


bin/rails test test/models/user_test.rb:9 

这是我的 user.rb 模型:

class User < ApplicationRecord
    before_save :email_downcase
    EMAIL_REGEXP =  /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
    validates :email, presence: true, length: { maximum: 255 },
                    format: { with: EMAIL_REGEXP },
                    uniqueness: { case_sensitive: false }

    private

        def email_downcase
            self.email = email.downcase
        end

end

这是我的 users_controller.rb 文件:

class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      UserMailer.account_activation(@user).deliver_now
      redirect_to root_path
      flash[:success] = "Welcome to Dave's Double Down Dogs!"
    else
      render 'new'
    end
  end

  private

    def user_params
      params.require(:user).permit(:email)
    end
end

这是我的 application_cpontroller.rb 文件:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
end

这是我的 users.yml 文件:

christian:
  email: christian@example.com

example:
  email: user@example.com

user:
  email: user1@example.com

这是我的 seeds.rb 文件:

User.create!(email: "example@railstutorial.org")

这是我的 user_test.rb 文件:

require 'test_helper'

class UserTest < ActiveSupport::TestCase

  def setup
    @user = User.new(email: "user@example.com")
  end

  test "user should be valid" do
    assert @user.valid?
  end

  test "user should have valid email" do
    assert @user.valid?  
  end

  test "emails hould be valid" do
    @user.email = "   "
    assert_not @user.valid?
  end

  test "email should not be too long" do
    @user.email = 'i' * 244 + '@example.com'
    assert_not @user.valid?
  end

  test "email addresses should be unique" do
    dup_user = @user.dup
    dup_user.email = @user.email.upcase
    @user.save
    assert_not dup_user.valid?
  end

  test "email validation should accept valid email addresses" do
    valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org
                     first.last@foo.jp alice+bob@baz.cn]
    valid_addresses.each do |i|
      @user.email = i
      assert @user.valid?
    end
  end

  test "email validation should reject invalid email addresses" do
    invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
                       foo@bar_baz.com foo@bar+baz.com]
    invalid_addresses.each do |i|
      @user.email = i
      assert_not @user.valid?
    end
  end

  test "email addresses should be saved in lowercase" do
    multi_case_email = "Foo@ExAMPle.CoM"
    @user.email = multi_case_email
    @user.save
    assert_equal multi_case_email.downcase, @user.reload.email
  end

end

这是我的 users_signup_test.rb 文件:

require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest

  test "invalid signup info" do
    get signup_path
    assert_no_difference 'User.count' do
      post signup_path, params: { user: { email: "" } }
    end
    assert_template 'users/new'
    assert_select 'div#error_explanation'
    assert_select 'div.field_with_errors'
  end

  test "valid signup info" do
    get signup_path
    assert_difference 'User.count', 1 do
      post signup_path, params: { user: { email: "user@example.com" } }
    end
    assert_redirected_to root_path
    flash[:success] 
  end

end

编辑:在意识到问题可能是由我

发送的重复电子邮件地址引起的之后
def setup
    @user = User.new(email: "user@example.com")
end

我的 user_test.rb 与我的 users.yml 文件交互的方法 我已将我的 users.yml 文件更改为:

christian:
  email: christian@example.gov

example:
  email: user@example.gov

user:
  email: userx@example.gov

但是,我现在得到的错误信息如下:

.E

Error:
UsersSignupTest#test_valid_signup_info:
NameError: uninitialized constant UsersController::UserMailer
    app/controllers/users_controller.rb:14:in `create'
    test/integration/users_signup_test.rb:18:in `block (2 levels) in <class:UsersSignupTest>'
    test/integration/users_signup_test.rb:17:in `block in <class:UsersSignupTest>'


bin/rails test test/integration/users_signup_test.rb:15

最后一个错误是由 users_controller 中的 UserMailer 引起的,将其更改为 DogMailer 修复了错误