這很奇怪嗎?我錯過了什麼魔法?Rspec返回錯誤的Ruby Date月份。 Ruby的類返回正確的
class Calendar
def initialize
@date = Time.new
end
def month
@date.strftime("%B")
end
def calendar
calendar = { 'month' => self.month }
end
end
c = Calendar.new
puts c.calendar => {"month"=>"February"}
calendar_spec.rb
require 'date'
require 'spec_helper'
require 'calendar'
describe Calendar do
subject { Calendar.new }
context "calendar" do
it "should save current month into calendar hash" do
expect(subject.calendar['month']).to eq(Date.new.strftime("%B"))
end
end
$> rspec spec
Failures:
1) Calendar calendar should save current month into calendar hash
Failure/Error: expect(subject.calendar['month']).to eq(Date.new.strftime("%B"))
expected: "January"
got: "February"
(compared using ==)
# ./spec/calendar_spec.rb:25:in `block (3 levels) in <top (required)>'