Last active
August 29, 2015 14:22
-
-
Save pigfly/94ac6c6d6a2ad511a734 to your computer and use it in GitHub Desktop.
Gist for my post "Rails: Model and Its Associated Testing"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateUsers < ActiveRecord::Migration | |
def change | |
create_table :users do |t| | |
t.string :name | |
t.string :email | |
t.timestamps null: false | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
#Some database adapters use case-sensitive indices, considering the | |
#strings “[email protected]” and “[email protected]” 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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
class UserTest < ActiveSupport::TestCase | |
def setup | |
@user = User.new(name: "Example User", email: "[email protected]") | |
end | |
test "should be valid" do | |
assert @user.valid? | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
class UserTest < ActiveSupport::TestCase | |
def setup | |
@user = User.new(name: "Example User", email: "[email protected]") | |
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 | |
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[[email protected] | |
[email protected] | |
[email protected] [email protected] | |
[email protected]] | |
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