失败:UsersSignupTest#test_valid_signup_information_with_account_activation预期:1实际:2

时间:2015-11-29 13:56:30

标签: ruby-on-rails

我正在关注railstutorial并在第10章中遇到此错误。 我认为应该与此post类似。不幸的是,该解决方案无法解决我的问题。

在user_signup_test.rb中将1更改为行2中的assert_equal 1, ActionMailer::Base.deliveries.size,使测试成为绿色。大声笑。 但我是一个完整的菜鸟,所以不要真正了解真正的问题。

有人能告诉我我想念的地方吗?

Failure:
UsersSignupTest#test_valid_signup_information_with_account_activation 
[/home/xxx/sample_app/test/integration/users_signup_test.rb:30]:
Expected: 1 
Actual: 2

user_signup_test.rb

require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest
  # test "the truth" do
  #   assert true
  # end
def setup
  ActionMailer::Base.deliveries.clear
end

test "invalid signup information" do
    get signup_path
    assert_no_difference 'User.count' do
      post users_path, user: { name:  "",
                               email: "user@invalid",
                               password:              "foo",
                               password_confirmation: "bar" }
    end
    assert_template 'users/new'
  end

  test "valid signup information with account activation" do
    get signup_path
    assert_difference 'User.count', 1 do
      post users_path, user: { name:  "Example User",
                               email: "user@example.com",
                               password:              "password",
                               password_confirmation: "password" }
    end
    assert_equal 1, ActionMailer::Base.deliveries.size
    user = assigns(:user)
    assert_not user.activated?
    # Try to log in before activation.
    log_in_as(user)
    assert_not is_logged_in?
    # Invalid activation token
    get edit_account_activation_path("invalid token")
    assert_not is_logged_in?
    # Valid token, wrong email
    get edit_account_activation_path(user.activation_token, email: 'wrong')
    assert_not is_logged_in?
    # Valid activation token
    get edit_account_activation_path(user.activation_token, email: user.email)
    assert user.reload.activated?
    follow_redirect!
    assert_template 'users/show'
    assert is_logged_in?
  end
    #assert_template 'users/show'
    #assert is_logged_in?

end

user_controller.rb

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:edit, :update, :index, :destroy ]
  before_action :correct_user,   only: [:edit, :update]
  before_action :admin_user,     only: :destroy

  def new
    @user = User.new
  end

  def index
    #@users = User.all
    @users = User.paginate(page: params[:page])
  end

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

  def create
    @user = User.new(user_params)    # Not the final implementation!
    if @user.save
      # Handle a successful save.
      #log_in @user
      #flash[:success] = "Welcome to the Sample App!"
      #redirect_to @user
      @user.send_activation_email
      UserMailer.account_activation(@user).deliver_now
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url

    else
      render 'new'
    end
  end

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

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      #Handle a successful update
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User deleted"
    redirect_to users_url
  end

private
def user_params
  params.require(:user).permit(:name, :email, :password, :password_confirmation)
end

# Before filters

# Confirms a logged-in user.
def logged_in_user
  unless logged_in?
    store_location
    flash[:danger] = "Please log in."
    redirect_to login_url
  end
end

# Confirms the correct user.
def correct_user
  @user = User.find(params[:id])
  #redirect_to root_url unless @user == current_user
  redirect_to root_url unless current_user?(@user)
end

# Confirms an admin user.
def admin_user
  redirect_to(root_url) unless current_user.admin?
end

end

user_mailer.rb

class UserMailer < ApplicationMailer

  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.user_mailer.account_activation.subject
  #
  def account_activation(user)
    @user = user
    mail to: user.email, subject: "Account activation"
  end

  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.user_mailer.password_reset.subject
  #
  def password_reset
    @greeting = "Hi"

    mail to: "to@example.org"
  end
end

application_mailer.rb

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

account_activation_controller.eb

class AccountActivationsController < ApplicationController

  def edit
    user = User.find_by(email: params[:email])
    if user && !user.activated? && user.authenticated?(:activation, params[:id])
      user.activate
      log_in user
      flash[:success] = "Account activated!"
      redirect_to user
    else
      flash[:danger] = "Invalid activation link"
      redirect_to root_url
    end
  end
end

session_controller.rb

class SessionsController < ApplicationController
  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
        if user.activated?
        log_in user
        params[:session][:remember_me] == '1' ? remember(user) : forget(user)
          #redirect_to user
        redirect_back_or(user)
      else
        message = "Account not activated."
        message += "Check your email for the activation link."
        flash[:warning] = message
        redirect_to root_url
      end
    else
    flash.now[:danger] = 'Invalid email/password combination' # Not quite right!
    render 'new'
    end
  end

  def destroy
    log_out if logged_in?
    log_out
    redirect_to root_url
  end

end

user_mailer_test.rb

要求'test_helper'

class UserMailerTest < ActionMailer::TestCase
  test "account_activation" do
    user = users(:michael)
    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 ["noreply@example.com"], mail.from
    assert_match "Hi", mail.body.encoded
  end

end

1 个答案:

答案 0 :(得分:1)

"cust_id".map {
  case '_' => " "
  case c   => c
}.mkString

如果没有看到其余的代码,这看起来应该发送两封单独的电子邮件。第二行肯定会发送电子邮件,但我们无法看到用户模型知道第一行正在做什么。

如果该代码确实发送了两封电子邮件,那么测试就会失败。