Skip to content

Instantly share code, notes, and snippets.

@ysnerdem
Created November 15, 2018 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ysnerdem/6f9416f7ff18fdcf7b021978ec5fd299 to your computer and use it in GitHub Desktop.
Save ysnerdem/6f9416f7ff18fdcf7b021978ec5fd299 to your computer and use it in GitHub Desktop.
one:
name: MyString
email: MyString
two:
name: MyString
email: MyString
valid:
name: ysn
email: ysn@kobiks.com
old:
name: Old
email: old@example.com
created_at: <%= 2.weeks.ago %>
#app/models/user.rb
class User < ApplicationRecord
scope :recent, -> { where('created_at > ?', 1.week.ago) }
has_many :posts
validates :name, :email, presence: true
end
#test/models/user_test.rb
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@user = users(:valid)
end
test 'valid user' do
assert @user.valid?
end
test 'invalid without name' do
@user.name = nil
refute @user.valid?
assert_not_nil @user.errors[:name]
end
test 'invalid without email' do
@user.email = nil
refute @user.valid?
assert_not_nil @user.errors[:email]
end
test '#posts' do
assert_equal 2, @user.posts.size
end
test '#recent' do
assert_includes User.recent, users(:valid)
refute_includes User.recent, users(:old)
end
end
one:
name: MyString
email: MyString
two:
name: MyString
email: MyString
valid:
name: ysn
email: ysn@kobiks.com
old:
name: Old
email: old@example.com
created_at: <%= 2.weeks.ago %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment