Skip to content

Instantly share code, notes, and snippets.

@ChunAllen
Last active January 26, 2024 08:00
Show Gist options
  • Save ChunAllen/d5099010c08f65f244b8e9e2bcc7be87 to your computer and use it in GitHub Desktop.
Save ChunAllen/d5099010c08f65f244b8e9e2bcc7be87 to your computer and use it in GitHub Desktop.
Unit Test Ruby on Rails app with RSpec
RSpec.describe Event, type: :model do
describe "validations" do
let(:event) { build(:event) }
it { should validate_presence_of(:title) }
it { should validate_presence_of(:category) }
it { should validate_presence_of(:location) }
it { should validate_presence_of(:start_date) }
it { should validate_presence_of(:end_date) }
it { should validate_presence_of(:status) }
describe '#date_range validation' do
# Context for Happy Path
context 'start_date earlier than end_date' do
it "should be valid" do
event.start_date = Date.today
event.end_date = 1.day.from_now
expect(event.save).to eq(true)
end
end
# context for Unhappy Path
context 'start_date is later than end_date' do
it "should be invalid" do
event.start_date = Date.today
event.end_date = 5.days.ago
expect(event.save).to eq(false)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment