【问题标题】:RSpec - expected equal?( Time Object ) to return true, got falseRSpec - 预期相等?(时间对象)返回真,得到假
【发布时间】:2014-02-17 16:58:25
【问题描述】:

我正在尝试(但失败了!)以下代码来比较 RSpec 中的两个 Time 对象:

describe '#current_shift_datetime' do
  subject { WipStack.current_shift_datetime }
  it "To be a Time object" do
    expect(subject).to be_kind_of(Time)
  end
  it "To return current shift datetime" do
    # Trying to figure out why they are not equal
    puts subject.in_time_zone.round
    puts 0.day.ago.midnight.in_time_zone.round

    # Problematic code here -->
    expect(subject.in_time_zone.round).to be_equal(0.day.ago.midnight.in_time_zone.round)
    # End of problematic code
  end
end

我在互联网上阅读了几篇关于 rspec 时间比较的文章,其中一篇解释了毫秒的问题(因此是回合),另一页则讨论了 stub,但后来我以 An error occurred in an after hook SystemStackError: stack level too deep 结束。

测试的输出是:

 1) WipStack#current_shift_datetime To return current shift datetime
    Failure/Error: expect(subject.in_time_zone.round).to be_equal(0.day.ago.midnight.in_time_zone.round)
    expected equal?(Mon, 17 Feb 2014 00:00:00 CST -06:00) to return true, got false

puts 输出:

#current_shift_datetime
  To be a Time object
  2014-02-17 00:00:00 -0600
  2014-02-17 00:00:00 -0600
  To return current shift datetime (FAILED - 1)

更新:

这里是current_shift_datetime 方法:

def WipStack.current_shift_datetime(scope = nil)
  shifts = WipStack.get_shifts(scope)
  current_shift = (Time.zone.now - 1.day).strftime("%m/%d/%Y")+" "+shifts.last
  current_time = Time.zone.now.strftime("%H:%M")
  shifts.each do |shift|
    if current_time > shift
      current_shift =  Time.zone.now.strftime("%m/%d/%Y")+" "+shift
    end
  end
  Time.zone.parse(current_shift)
end

【问题讨论】:

  • 我们来看看current_shift_datetime方法
  • 谢谢@CharlesJHardy 这是代码

标签: ruby-on-rails ruby ruby-on-rails-3 rspec


【解决方案1】:

与其直接比较时区(并陷入细微的差异),您可以尝试将字符串表示相互比较,因为无论如何这就是您在控制台中检查的内容。

根据您在应用中使用此时区的方式,这足以说明它们基本上是同一时间。

expect(subject.in_time_zone.round.to_s).to eq(0.day.ago.midnight.in_time_zone.round.to_s)

如果您愿意,也可以删除 .round

编辑

尝试更改您的代码,使打印的内容和比较的内容没有区别:

it "To return current shift datetime" do
  a = subject.in_time_zone.round.to_s
  b = 0.day.ago.midnight.in_time_zone.round.to_s

  puts a
  puts b

  expect(a).to eq(b)
end

【讨论】:

  • 现在我很困惑,因为我真的认为这会解决它,但出于某种原因,我不明白这给了我同样的错误...1) WipStack#previous_shift_datetime To return last shift datetime Failure/Error: expect(subject.to_s).to be_equal(1.day.ago.midnight.to_s) expected equal?("2014-02-16 00:00:00 -0600") to return true, got false
  • 按说是同一个字符串To be a Time object 2014-02-16 00:00:00 -0600 2014-02-16 00:00:00 -0600
  • 太好了,很高兴为您提供帮助。请随意将其标记为正确。
  • 我会放弃,但使用 equal 测试失败
【解决方案2】:

感谢tyler!,我修复了它似乎 be_equal 实际上是在尝试查看两个对象是否相同(比较字符串时测试失败时出现注释),将 be_equal 更改为 eq 使测试通过!

it "To return current shift datetime" do
  #pending
  puts subject.in_time_zone.round.to_s
  puts 0.day.ago.midnight.in_time_zone.round.to_s
  expect(subject.in_time_zone.round.to_s).to eq(0.day.ago.midnight.in_time_zone.round.to_s)
end

【讨论】:

  • 请接受答案而不是复制它
猜你喜欢
  • 1970-01-01
  • 2022-09-28
  • 2016-06-12
  • 2016-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多