Hi I am creating my User
which the User
requirements are: 1. to have a User.sponsor_id
with an exeption of the first user does not require to have a sponsor_id
. 2. the sponsor_id must be exist on User.
I have this code right now for the model/user.rb
:
class User < ActiveRecord::Base
belongs_to :sponsor, class_name: "User"
attr_accessor :remember_token
before_save {email.downcase!}
validates :first_name, :presence =>true, length: {maximum: 50}
validates :last_name, :presence => true, length: {maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, :presence =>true, length: {maximum: 255}, format: {with: VALID_EMAIL_REGEX }, uniqueness: {case_sensitive: false }
has_secure_password
validates :password, presence: true, length: {minimum: 6}, allow_nil:true
validates :sponsor, presence:true
validate :sponsor_id_valid
.
.
.
def sponsor_id_valid
if self.sponsor_id.blank? && User.count!=User.first
errors.add(:sponsor_id, "is not exist")
end
end
end
And I have this code for my db/migrate/...usertable.rb
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :email, unique: true
t.integer :sponsor_id
t.timestamps null: false
end
add_index :users, :sponsor_id
end
end
And this is my db/seeds.rb
file
User.create!(first_name: "Test",
last_name: "User",
email: "test@user.com",
password: "password",
password_confirmation: "password",
sponsor_id: 1 )
10.times do |n|
first_name = Faker::Name.name
last_name=Faker::Name.name
email = "example-#{n+1}@user.com"
password = "password"
User.create!(first_name: first_name,
last_name: last_name,
email: email,
password: password,
password_confirmation: password,
sponsor_id:test.id )
end
The test code I want to past is in test/model/users.rb
:
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@user=User.first
@user2=User.new(
first_name: "Test2",
last_name: "User2",
email: "user2@test.com",
sponsor_id: @user.id,
password:"foobar",
password_confirmation:"foobar")
@user2.save
end
test "1. should be valid" do
byebug
assert @user.valid?
end
test "2. First name should be present" do
@user.first_name = ""
assert_not @user.valid?
end
test "3. Last name should be present" do
@user.last_name=""
assert_not @user.valid?
end
test "4. email should be present" do
@user.email=""
assert_not @user.valid?
end
test "5. sponsor_id should be present" do
@user2.sponsor_id= nil
assert_not @user2.valid?
end
test "6.name should not be too long" do
@user.first_name= "a" * 52
@user.last_name="a" * 52
assert_not @user.valid?
end
test "7. email should not be too long " do
@user.email="a" * 244+ "@example.com"
assert_not @user.valid?
end
test "8.email validation should accept valid addresses" do
valid_addresses= %w[userunique@test.com USER@foo.COM A_US-ER@foo.bar.org first.last@foo.jp alice+bob@bax.cn]
valid_addresses.each do |valid_address|
@user.email = valid_address
assert @user.valid?, "#{valid_address.inspect} should be valid"
end
end
test "9. email validation should reject invalid addresses" do
invalid_addresses = %w[user@example,com user_at_foo.org user.name@example. foo@bar_baz.com foo@bar+baz.com foo@bar..com]
invalid_addresses.each do |invalid_address|
@user.email = invalid_address
assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"
end
end
test "10. Email addresses should be unique" do
duplicate_user=@user.dup
duplicate_user.email= @user.email.upcase
@user.save
assert_not duplicate_user.valid?
end
test "11. password should be present (nonblank" do
@user.password = @user.password_confirmation = " " * 6
assert_not @user.valid?
end
test "12. password should have a minimum length" do
@user.password = @user.password_confirmation = "a" * 5
assert_not @user.valid?
end
test "13. email addresses should be saved as lower-case" do
mixed_case_email = "PEaK@ExAMPle.CoM"
@user.email = mixed_case_email
@user.save
assert_equal mixed_case_email.downcase, @user.reload.email
end
test "14.sponsor_id must be presence" do
@user2.sponsor_id= nil
assert_not @user2.valid?
end
test "15. the sponsor_id must be the existing id" do
@user2.sponsor_id = 1000
assert_not @user2.valid?
end
test "16. authenticated? should return false for a suer with nil digest" do
assert_not @user.authenticated?('')
end
end
When I run rake db:seed
it always said I have wrong argument Can anyone help me? Please advise.
Thanks in advance
PS @RichPeck (I don't know how to tag) but this is the upgraded version
Aucun commentaire:
Enregistrer un commentaire