Slide 40
Slide 40 text
# ѱ͍ྫ
class Post < ApplicationRecord
# όάؚ͕·Ε͍ͯΔ
scope :last_month_published, -> { where(publish_at: (Time.zone.now - 31.days).all_month) }
end
RSpec.describe Post, type: :model do
describe '.last_month_published' do
let!(:april_1st) { create :post, publish_at: Time.zone.local(2017, 4, 1) }
let!(:april_30th) { create :post, publish_at: Time.zone.local(2017, 4, 30) }
before do
create :post, publish_at: Time.zone.local(2017, 5, 1)
create :post, publish_at: Time.zone.local(2017, 3, 31)
end
it 'return published posts in last month' do
Timecop.travel(2017, 5, 6) do
# ςετ௨Δ
expect(Post.last_month_published).to contain_exactly(april_1st, april_30th)
end
end
end
end