我正在使用Rails 5.2.2.1和Rspec。 拜托,我该如何安排时间让此测试始终保持绿色?
很少修改,我根据建议添加了Timecop并更新了我的问题,但仍然失败
今天我的测试失败了,因为它是一个月的第一天...
before(:each) do
Timecop.freeze(Time.local(2019, 03, 31, 12, 0, 0))
end
let(:date_from) { Timecop.freeze(Time.local(2019, 03, 26, 12, 0, 0)) }
let(:date_to) { Timecop.freeze(Time.local(2019, 03, 31, 12, 0, 0)) }
subject { Dashboard.new(date_from: @date_from, date_to: @date_to) }
describe "#sales_by_week" do
it "returns the number of sales by week" do
create(:sale, created_at: 2.weeks.ago)
create(:sale, created_at: 3.days.ago)
create(:sale, created_at: Date.today )
res = subject.sales_by_week
expect(res.values.last).to eq(2)
end
end
describe "#sales_by_month" do
it "returns the number of sales by month" do
create(:sale, created_at: 2.day.ago)
create(:sale, created_at: 1.day.ago)
res = subject.sales_by_month
expect(res.values.last).to eq(2)
end
end
这是我正在测试的方法:
class Dashboard
attr_reader :date_from, :date_to
def initialize(params)
params ||= {}
@date_from = parsed_date(params[:date_from],Time.now.beginning_of_month.to_date.to_s)
@date_to = parsed_date(params[:date_to], (Date.today + 1).to_s)
end
def sales_by_week
Sale.group_by_week(:created_at, range: (@date_from..@date_to),time_zone: "Paris", week_start: :mon).count
end
def sales_by_month
Sale.group_by_month(:created_at, range: (@date_from..@date_to),time_zone: "Paris", week_start: :mon).count
end
end