Skip to content

Commit cc7ef8e

Browse files
authored
Merge pull request TelosLabs#185 from TelosLabs/session-specs
Add Session model specs
2 parents 9b6501b + 07c7619 commit cc7ef8e

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

app/models/session.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def live?
6666
def starting_soon?
6767
return false if starts_at < Time.current
6868

69-
(starts_at - Time.current) < 1.hour
69+
(starts_at - Time.current) <= 1.hour
7070
end
7171

7272
def past?

spec/models/session_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626

2727
RSpec.describe Session, type: :model do
2828
let(:session) { build_stubbed(:session) }
29+
let(:live_session) { create(:session, :live) }
30+
let(:starting_soon_session) { create(:session, :starting_soon) }
31+
let(:past_session) { create(:session, :past) }
32+
33+
before do
34+
Timecop.freeze(Time.current.change(hour: 12))
35+
end
2936

3037
it "has a valid factory" do
3138
expect(session).to be_valid
@@ -39,4 +46,51 @@
3946
expect(session).not_to be_valid
4047
end
4148
end
49+
50+
describe "#live?" do
51+
it { expect(live_session).to be_live }
52+
53+
context "when session has not started" do
54+
it "returns false" do
55+
allow(live_session).to receive_messages(starts_at: 1.hour.from_now, ends_at: 2.hours.from_now)
56+
expect(live_session).not_to be_live
57+
end
58+
end
59+
60+
context "when session has already ended" do
61+
it "returns false" do
62+
allow(live_session).to receive_messages(starts_at: 2.hours.ago, ends_at: 1.hour.ago)
63+
expect(live_session).not_to be_live
64+
end
65+
end
66+
end
67+
68+
describe "#starting_soon?" do
69+
it { expect(starting_soon_session).to be_starting_soon }
70+
71+
context "when session has already started" do
72+
it "returns false" do
73+
allow(starting_soon_session).to receive(:starts_at).and_return(2.hours.ago)
74+
expect(starting_soon_session).not_to be_starting_soon
75+
end
76+
end
77+
78+
context "when session starts in more than an hour" do
79+
it "returns false" do
80+
allow(starting_soon_session).to receive(:starts_at).and_return(2.hours.from_now)
81+
expect(starting_soon_session).not_to be_starting_soon
82+
end
83+
end
84+
end
85+
86+
describe "#past?" do
87+
it { expect(past_session).to be_past }
88+
89+
context "when session hasn't ended" do
90+
it "returns false" do
91+
allow(past_session).to receive(:ends_at).and_return(2.hours.from_now)
92+
expect(past_session).not_to be_past
93+
end
94+
end
95+
end
4296
end

0 commit comments

Comments
 (0)