How to check the creation of a user? rails4

please help correct test.

its a model User:
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
before_create :create_remember_token

VALID_EMAIL_REGEX = /\A[\w+-.][email protected][a-z\d-.]+.[a-z]+\z/i
validates :email, presence: true,
format: { with:
VALID_EMAIL_REGEX },
uniqueness: { case_sensitive:
false }
validates :diary_name, presence: true, uniqueness: { case_sensitive:
false }
validates :password, length: { minimum: 6 }
validates :name, presence: true, length: { maximum: 50 },
uniqueness: { case_sensitive: false }

has_secure_password

has_many :recalls
has_many :posts, dependent: :destroy
belongs_to :gender

has_attached_file :avatar, :styles => { :large => “300x300>”, :medium
=> “100x100>”, :thumb => “30x30>” }
validates_attachment_content_type :avatar, :content_type =>
[“image/jpg”, “image/jpeg”, “image/png”, “image/gif”]
validates_attachment_file_name :avatar, :matches => [/png\Z/,
/jpe?g\Z/, /gif\Z/]
end

its a controller UserController:
class UsersController < ApplicationController
def new
@user = User.new
end

def create
@user = User.new(user_params)

if  @user.save
  sign_in @user
  flash[:success] = "Welcome  to  the Sample  App!"
  redirect_to  @user
else
  flash.now[:error] = 'Invalid data'
  render  'new'
end

end

private

def user_params
  params.require(:user).permit(:name, :email, :diary_name,

:password, :gender_id, :password_confirmation, :phone, :skype, :info,
:avatar, :delete_avatar)
end
end

its a fixture user:
one:
name: ‘onfge’
email: ‘[email protected]
password_digest:
‘$2a$10$XS2HLwMZxg/7yRKAWd9AJ.afCMra0wGWK4b.FhkY/qo3Lmo/tKEiO’
remember_token: ‘dc3461e13c8d316dad22332a503e06edafa0b9cb’
phone: ‘43535345’
skype: ‘gggggg’
gender: one
info: ‘MyString’
admin: true
avatar_file_name: nil
avatar_content_type: nil
avatar_file_size: nil

its a fixture genders:
one:
gender_name: ‘-’

two:
gender_name: ‘male’

three:
gender_name: ‘female’

a test that does not work:
class UsersControllerTest < ActionController::TestCase
fixtures :users

setup do
@user = users(:one)
@input_attributes = {
email: ‘[email protected]’,
gender_id: 1,
info: ‘u76u67u67u’,
name: ‘fsdfsdf’,
password: ‘qwerty’,
password_confirmation: ‘qwerty’,
phone: ‘435345345’,
skype: ‘sdggdfgdfgfdgd’
}
end

test “should create user” do
assert_difference(‘User.count’) do
post :create,
user: @input_attributes
end
end
end

after a test run in the console I get the following error message:

  1. Failure:
    UsersControllerTest#test_should_create_user
    [/home/kalinin/rails/ZSUM/test/controllers/users_controller_test.rb:37]:
    “User.count” didn’t change by 1.
    Expected: 3
    Actual: 2

It is the message that is logged. it is clear that the transaction is
rolled back for some strange reason:
[1m[35mUser Load (0.1ms)[0m SELECT “users”.* FROM “users” WHERE
“users”.“id” = ? LIMIT 1 [[“id”, 980190962]]
[1m[36m (0.1ms)[0m [1mSELECT COUNT(*) FROM “users”[0m
Processing by UsersController#create as HTML
Parameters: {“user”=>{“email”=>“[email protected]”, “gender_id”=>“1”,
“info”=>“u76u67u67u”, “name”=>“fsdfsdf”, “password”=>"[FILTERED]",
“password_confirmation”=>"[FILTERED]", “phone”=>“435345345”,
“skype”=>“sdggdfgdfgfdgd”}}
[1m[35m (0.1ms)[0m SAVEPOINT active_record_1
[1m[36mUser Exists (0.1ms)[0m [1mSELECT 1 AS one FROM “users” WHERE
LOWER(“users”.“email”) = LOWER(‘[email protected]’) LIMIT 1[0m
[1m[35mUser Exists (0.1ms)[0m SELECT 1 AS one FROM “users” WHERE
“users”.“diary_name” IS NULL LIMIT 1
[1m[36mUser Exists (0.1ms)[0m [1mSELECT 1 AS one FROM “users” WHERE
LOWER(“users”.“name”) = LOWER(‘fsdfsdf’) LIMIT 1[0m
[1m[35m (0.1ms)[0m ROLLBACK TO SAVEPOINT active_record_1

On 26 June 2015 at 17:48, Zlodiak Z. [email protected] wrote:


after a test run in the console I get the following error message:

  1. Failure:
    UsersControllerTest#test_should_create_user
    [/home/kalinin/rails/ZSUM/test/controllers/users_controller_test.rb:37]:
    “User.count” didn’t change by 1.
    Expected: 3
    Actual: 2

As has been suggested previously, probably one of your validations is
failing.
You can use logger.info(“some string”) to insert output into the log
file. If you put those at appropriate points in the controller create
method you can find whether save is failing. Then you can investigate
which validation is failing.

Colin

Just gave this a quick look…
What strikes me as odd is this in your User validation:
validates :name, presence: true, length: { maximum: 50 },uniqueness:
{
case_sensitive: false }
You want the user name unique yet you do not care about case
sensitivity?
CAT, cat, Cat, caT…
And then you query for: SELECT 1 AS one FROM “users” WHERE
LOWER(“users”.“name”) = LOWER(‘fsdfsdf’) LIMIT… That doesn’t make
sense
to me…
This strikes me as bad form. Make name case_sensitive: true. And in
your
SQL, remove LOWER.

Hope this helps…

Liz

On 28 June 2015 at 03:51, Elizabeth McGurty [email protected] wrote:

Just gave this a quick look…
What strikes me as odd is this in your User validation:
validates :name, presence: true, length: { maximum: 50 },uniqueness: {
case_sensitive: false }
You want the user name unique yet you do not care about case sensitivity?
CAT, cat, Cat, caT…

I guess the OP means that if there already is a use CAT then he does
not want to allow another user to be called cat. Not an unreasonable
idea I think.

And then you query for: SELECT 1 AS one FROM “users” WHERE
LOWER(“users”.“name”) = LOWER(‘fsdfsdf’) LIMIT… That doesn’t make sense to
me…
This strikes me as bad form. Make name case_sensitive: true. And in your
SQL, remove LOWER.

The LOWER is used by rails enforce the case insensitivity, it is not
something the OP has done explicitly.

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs