These are the errors it shows, however, it does work on the system:
UsersControllerTest#test_should_destroy_user [/mnt/c/code/toy_application/test/controllers/users_controller_test.rb:42]:
"User.count" didn't change by -1.
Expected: 1
Actual: 2
Failure:
UsersControllerTest#test_should_destroy_user [/mnt/c/code/toy_application/test/controllers/users_controller_test.rb:42]:
"User.count" didn't change by -1.
Expected: 1
Actual: 2
Failure:
UsersControllerTest#test_should_get_edit [/mnt/c/code/toy_application/test/controllers/users_controller_test.rb:33]:
Expected response to be a <2XX: success>, but was a <302: Found> redirect to <http://www.example.com/login>
Response body: <html><body>You are being <a href="http://www.example.com/login">redirected</a>.</body></html>
UsersControllerTest#test_should_update_user [/mnt/c/code/toy_application/test/controllers/users_controller_test.rb:38]:
Expected response to be a redirect to <http://www.example.com/users/775686554> but was a redirect to <http://www.example.com/login>.
Expected "http://www.example.com/users/775686554" to be === "http://www.example.com/login".
user_controller.test
require "test_helper"
class UsersControllerTest < ActionDispatch::IntegrationTest
setup do
@user = users(:manon)
end
test "should get index" do
get users_url
assert_response :success
end
test "should get new" do
get signup_path
assert_response :success
end
test "should create user" do
assert_difference("User.count") do
post users_url, params: { user: { email: "user@example.com", name: "Example User", password: "thisfoobar", password_confirmation:"thisfoobar"} }
end
assert_redirected_to user_url(User.last)
end
test "should show user" do
get user_url(@user)
assert_response :success
end
test "should get edit" do
get edit_user_url(@user)
assert_response :success
end
test "should update user" do
patch user_url(@user), params: { user: { email: @user.email, name: @user.name, password: "thisfoobar", password_confirmation: "thisfoobar" } }
assert_redirected_to user_url(@user)
end
test "should destroy user" do
assert_difference("User.count", -1) do
delete user_url(@user)
end
assert_redirected_to users_url
end
end
user_controller:
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index,:edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
# GET /users or /users.json
def index
@users = User.all
end
# GET /users/1 or /users/1.json
def show
@user = User.find(params[:id])
# debugger
end
# GET /users/new
def new
@user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users or /users.json
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
log_in @user
format.html { redirect_to user_url(@user), notice: "Welcome to the Sample App" }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def edit
@user = User.find(params[:id])
end
# def update
# @user = User.find(params[:id])
# if @user.update(user_params)
# # Handle a successful update.
# else
# render 'edit'
# end
# end
# PATCH/PUT /users/1 or /users/1.json
def update
if @user.update(user_params)
flash[:success] = "Profile Updated"
redirect_to user_url(@user)
else
flash.now[:error] = "There was an error updating your profile."
render :edit, status: :unprocessable_entity
end
end
# DELETE /users/1 or /users/1.json
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted"
redirect_to users_url
end
#show micropost for the user
def show
@user = User.find(params[:id])
@micropost = @user.microposts.first
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Only allow a list of trusted parameters through.
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 current_user?(@user)
end
# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.admin?
end
end
user.rb
class User < ApplicationRecord
has_many :microposts
attr_accessor :remember_token
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: true
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
# Returns the hash digest of the given string.
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
# Returns a random token for the advanced login(remember me functionality).
def User.new_token
SecureRandom.urlsafe_base64
end
# Remembers a user in the database for use in persistent sessions.
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end
# Returns true if the given token matches the digest.
def authenticated?(remember_token)
return false if remember_digest.nil?
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end
# Forgets a user.
def forget
update_attribute(:remember_digest, nil)
end
end
Gemfile
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby "3.2.0"
# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.4", ">= 7.0.4.2"
#bootstrap
gem "bootstrap", "~> 5.2.0"
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"
# Use postgresql as the database for Active Record
gem "pg", "~> 1.1"
# Use the Puma web server [https://github.com/puma/puma]
gem "puma", "~> 5.0"
# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem "importmap-rails"
# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"
# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"
# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder"
# Use Redis adapter to run Action Cable in production
# gem "redis", "~> 4.0"
# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]
# gem "kredis"
# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
gem "bcrypt", "~> 3.1.7"
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false
# Use Sass to process CSS
# gem "sassc-rails"
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"
group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri mingw x64_mingw ]
end
group :development do
# Use console on exceptions pages [https://github.com/rails/web-console]
gem "web-console"
# Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
# gem "rack-mini-profiler"
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
end
group :test do
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
gem "capybara"
gem "selenium-webdriver"
gem "webdrivers"
end
What does the error mean and how do I fix the error?
Aucun commentaire:
Enregistrer un commentaire