-
-
Save dogweather/1987ffb0b464e2eab5c9 to your computer and use it in GitHub Desktop.
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 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 |
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 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 |
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
bond: | |
first_name: James | |
last_name: Bond | |
email: [email protected] | |
mendez: | |
first_name: Tony | |
last_name: Mendez | |
email: [email protected] | |
bourne: | |
first_name: Jason | |
last_name: Bourne | |
email: [email protected] |
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
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 |
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
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