Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Session model specs #185

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def live?
def starting_soon?
return false if starts_at < Time.current

(starts_at - Time.current) < 1.hour
(starts_at - Time.current) <= 1.hour
end

def past?
Expand Down
54 changes: 54 additions & 0 deletions spec/models/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@

RSpec.describe Session, type: :model do
let(:session) { build_stubbed(:session) }
let(:live_session) { create(:session, :live) }
let(:starting_soon_session) { create(:session, :starting_soon) }
let(:past_session) { create(:session, :past) }

before do
Timecop.freeze(Time.current.change(hour: 12))
end

it "has a valid factory" do
expect(session).to be_valid
Expand All @@ -39,4 +46,51 @@
expect(session).not_to be_valid
end
end

describe "#live?" do
it { expect(live_session).to be_live }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this allow us to check that the factory is creating the resource correctly


context "when session has not started" do
it "returns false" do
allow(live_session).to receive_messages(starts_at: 1.hour.from_now, ends_at: 2.hours.from_now)
expect(live_session).not_to be_live
end
end

context "when session has already ended" do
it "returns false" do
allow(live_session).to receive_messages(starts_at: 2.hours.ago, ends_at: 1.hour.ago)
expect(live_session).not_to be_live
end
end
end

describe "#starting_soon?" do
it { expect(starting_soon_session).to be_starting_soon }

context "when session has already started" do
it "returns false" do
allow(starting_soon_session).to receive(:starts_at).and_return(2.hours.ago)
expect(starting_soon_session).not_to be_starting_soon
end
end

context "when session starts in more than an hour" do
it "returns false" do
allow(starting_soon_session).to receive(:starts_at).and_return(2.hours.from_now)
expect(starting_soon_session).not_to be_starting_soon
end
end
end

describe "#past?" do
it { expect(past_session).to be_past }

context "when session hasn't ended" do
it "returns false" do
allow(past_session).to receive(:ends_at).and_return(2.hours.from_now)
expect(past_session).not_to be_past
end
end
end
end