我正在使用rails中的rspec测试模型(control.rb),我遇到了日期时间问题。它没有成功,因为我期望和我得到的差异为00001000000000。这是我的代码
describe ".graphic_data_for_all_days" do
before do
Timecop.freeze(DateTime.now)
end
after do
Timecop.return
end
it "the controls are ordered from older to newer" do
# time = DateTime.now.to_i
# DateTime.stub!(:now).and_return{DateTime.at(time += 5) }
control = FactoryGirl.create(
:control,
user: user,
level: 120,
day: Timecop.freeze(DateTime.now)
)
control2 = Control.create(
user_id: user.id,
level: 106,
period: "pre-breakfast",
day: Timecop.freeze(DateTime.now + 3.hours)
)
control3 = Control.create(
user_id: user.id,
level: 104,
period: "pre-breakfast",
day: Timecop.freeze(DateTime.now + 6.hours)
)
control4 = Control.create(
user_id: user.id,
level: 104,
period: "pre-breakfast",
day: Timecop.freeze(DateTime.now + 9.hours)
)
graphic_data_for_all_days = Control.graphic_data_for_all_days(user.id)
expect(graphic_data_for_all_days).to eq ([{
name: "day",
data:[
[Timecop.freeze(DateTime.now).strftime("%I:%M%p").to_time.utc.to_i * 1000, 120],
[(Timecop.freeze(DateTime.now + 3.hours)).strftime("%I:%M%p").to_time.utc.to_i * 1000, 106],
[(Timecop.freeze(DateTime.now + 6.hours)).strftime("%I:%M%p").to_time.utc.to_i * 1000, 104],
[(Timecop.freeze(DateTime.now + 9.hours)).strftime("%I:%M%p").to_time.utc.to_i * 1000, 104]
]
}])
end
这是config / environments / test.rb
config.after_initialize do
# Set Time.now to September 1, 2008 10:05:00 AM (at this instant), but allow it to move forward
t = DateTime.now
Timecop.travel(t)
end
这就是控制台让我回来的原因
expected: [{:name=>"day", :data=>[[1418881560000, 120], [1418892360000, 106], [1418913960000, 104], [1418946360000, 104]]}]
got: [{:name=>"day", :data=>[[1418899560000, 120], [1418910360000, 106], [1418931960000, 104]]}, {:name=>"day", :data=>[[1418877960000, 104]]}]
我不知道如何通过测试。如果有人能帮助我,我会感谢你。