Question

Good afternoon,

I've done a scaffold of a class, here the example: User - At the moment I was executing the test I saw this bug:

Minitest::Assertion: "User.count" didn't change by 1.
Expected: 3
  Actual: 2
test/controllers/users_controller_test.rb:20:in `block in <class:UsersControllerTest>'

the refering code of the bug is this one:

test "should create user" do
  assert_difference('User.count') do
    post :create, user: { name: 'test', password: 'secret', password_confirmation: 'secret' }
  end

  assert_redirected_to user_path(assigns(:user))
end

So the code above was created by the scaffold, I just change the name reference.

UserController:

class UsersController < ApplicationController

  before_action :set_user, only: [:show, :edit, :update, :destroy, :reset_password]

  # GET /users
  # GET /users.json
  def index
    @users = super
  end

  # GET /users/1
  # GET /users/1.json
  def show
  end

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)
    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: get_action_message }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    @user.update(user_params)
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: get_action_message }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url, notice: get_action_message }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:name, :password, :password_confirmation)
    end

    def search_params
      super - ['password_digest']
    end

    def show_attributes
      @show_attributes = super - ['password_digest']
    end

end

UserModel

class User < ActiveRecord::Base

  nilify_blanks

  validates :name, presence: true, uniqueness: true;

  has_secure_password

end

For what I was able to understand, the "create" it's not being executed. I've put the breakpoint on the Controller create but its not stoping there..

What am I doing wrong, besides my poor english skills?

Tks for the helping!!

Was it helpful?

Solution

Thks guys,

I found this bug.

I had forgotten to put in the user Session

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top