Skip to content

Instantly share code, notes, and snippets.

@genericsteele
Last active September 18, 2018 03:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save genericsteele/9468611 to your computer and use it in GitHub Desktop.
Save genericsteele/9468611 to your computer and use it in GitHub Desktop.
Custom Assertions
class Agent < ActiveRecord::Base
validates :first_name, :last_name, :email, presence: true
validates :email, uniqueness: true
has_many :missions
scope :on_assignment, -> { Agent.joins(:missions).where(missions: { status: 'active' }) }
scope :not_on_assignment, -> { where.not(id: Agent.on_assignment.pluck(:id)) }
def name
"#{first_name} #{last_name}"
end
def on_assignment?
missions.active.any?
end
def contract_end_date
created_at.to_date + 5.years
end
def days_active
Date.today - created_at.to_date
end
end
require 'test_helper'
class AgentTest < ActiveSupport::TestCase
test 'has valid test data' do
assert_valid agents(:bond)
end
test 'must have an email first_name and last_name' do
invalid_agent = Agent.new
assert_invalid invalid_agent
assert_includes invalid_agent.errors[:email], "can't be blank"
assert_includes invalid_agent.errors[:first_name], "can't be blank"
assert_includes invalid_agent.errors[:last_name], "can't be blank"
end
test 'must have a unique email' do
copy_cat = Agent.new(email: agents(:bond).email)
assert_invalid copy_cat
assert_includes copy_cat.errors[:email], 'has already been taken'
end
test 'on_assignment collects agents on active missions' do
agents_on_assigment = Agent.on_assignment
assert_includes agents_on_assigment, agents(:bond)
refute_includes agents_on_assigment, agents(:bourne)
end
test 'not_on_assignment collects agents not on active missions' do
agents_not_on_assigment = Agent.not_on_assignment
assert_includes agents_not_on_assigment, agents(:bourne)
refute_includes agents_not_on_assigment, agents(:bond)
end
test 'name combines the first and last name' do
assert_equal 'James Bond', agents(:bond).name
end
test 'on_assignment? should be true if an agent is on an active mission' do
assert agents(:bond).on_assignment?, ':bond should be on an active mission'
end
test 'on_assignment? should be false if an agent is not on an active mission' do
refute agents(:bourne).on_assignment?, ':bourne should not be on an active mission'
end
test 'contract_end_date should be 5 years after creation date' do
assert_equal 5.years.from_now.to_date, agents(:bond).contract_end_date
end
test 'days_active returns the number of days since created_at' do
agents(:bond).created_at -= 20.days
assert_equal 20, agents(:bond).days_active
end
end
bond:
first_name: James
last_name: Bond
email: jamesbond007@mi6.gov.uk
mendez:
first_name: Tony
last_name: Mendez
email: amendez@cia.gov
bourne:
first_name: Jason
last_name: Bourne
email: jbourne@cia.gov
thunderball:
code_name: Operation Thunderball
description: Recover the atomic bombs stolen by SPECTRE.
status: active
agent: bond
argo:
code_name: Argo
description: Extract hostages in hiding from Iran.
status: completed
agent: mendez
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
fixtures :all
# Add more helper methods to be used by all tests here...
def assert_valid(record, message = nil)
message ||= "Expected #{record.inspect} to be valid"
assert record.valid?, message
end
def assert_invalid(record, options = {})
assert record.invalid?, "Expected #{record.inspect} to be invalid"
options.each do |attribute, message|
assert_includes record.errors[attribute], message
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment