gpt4 book ai didi

Ruby time.new 与 time.now

转载 作者:数据小太阳 更新时间:2023-10-29 07:35:47 26 4
gpt4 key购买 nike

testfirst.org Performance Monitor rspec

我正在处理由 testfirst.org 提供的上述问题。下面是我的代码,所有测试都通过了:

def measure(iterate = 1)
timestamp = Time.now
iterate.times { yield }
timestamp_end = Time.now
(timestamp_end - timestamp) / iterate
end

但是,如果我将 Time.now 替换为 Time.new,一些测试会失败。我读到 Time.now 是 Time.new 的别名。那么,为什么测试会失败?使用这两种方法有什么区别?

最佳答案

这是因为测试用例stubs .now 而不是 .new 的返回值:

Time.stub(:now) { @eleven_am }

由于测试期望 @eleven_am 作为返回值,因此它们失败了。您必须对 Time.new 进行 stub ,但谁知道还有什么其他破坏可能会破坏。

关于Ruby time.new 与 time.now,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30689163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com