Skip to content

Instantly share code, notes, and snippets.

@pigfly
Last active August 29, 2015 14:22
Show Gist options
  • Save pigfly/94ac6c6d6a2ad511a734 to your computer and use it in GitHub Desktop.
Save pigfly/94ac6c6d6a2ad511a734 to your computer and use it in GitHub Desktop.
Gist for my post "Rails: Model and Its Associated Testing"
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps null: false
end
end
end
rails g model User name:string email:string
invoke active_record
create db/migrate/20140724010738_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
ActiveRecord::Schema.define(version: 20150530101243) do
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
class User < ActiveRecord::Base
#Some database adapters use case-sensitive indices, considering the
#strings “Foo@ExAMPle.CoM” and “foo@example.com” to be distinct, but
#our application treats those addresses as the same
before_save {self.email = self.email.downcase}
validates :name, presence: true, length: { maximum: 56 }
# see http://www.rubular.com/ for regex parser
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: { case_sensitive: false }
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@user = User.new(name: "Example User", email: "user@example.com")
end
test "should be valid" do
assert @user.valid?
end
end
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@user = User.new(name: "Example User", email: "user@example.com")
end
test "should be valid" do
assert @user.valid?
end
test "example user should be valid" do
assert @user.valid?
end
test "name should be present" do
@user.name = " "
assert_not @user.valid?
end
test "email should be present" do
@user.email = " "
assert_not @user.valid?
end
test "name should not be too long" do
@user.name = "long" * 20
assert_not @user.valid?
end
# email
test "email should not be too long" do
@user.email = "long" * 70
assert_not @user.valid?
end
test "email validation should accept valid email address" do
valid_email_addresses = %w[user@gmail.com
User@foD.coM
A_user-d@x.com alice+bob@dd.cn
firtst.last@y.au]
valid_email_addresses.each do |address|
@user.email = address
assert @user.valid?, "#{address} should be valid"
end
end
test "email validation should not accept invalid email address" do
invalid_email_addresses = %w[x.d@exx,com
user.name@xample
fag@example.
foo@bar+x.com
foo@bar_x.com]
invalid_email_addresses.each do |address|
@user.email = address
assert_not @user.valid?, "#{address} should be invalid !"
end
end
test "email address should be unique" do
duplicated_user = @user.dup
# the duplicate user has an email address that already exists in the database
@user.save
assert_not duplicated_user.valid?, "#{duplicated_user.email} already exists !"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment