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/