小编Pat*_*ngo的帖子

Stpecbing Time.now与RSpec

我试图在RSpec中存根Time.now,如下所示:

it "should set the date to the current date" do
    @time_now = Time.now
    Time.stub!(:now).and_return(@time_now)

    @thing.capture_item("description")
    expect(@thing.items[0].date_captured).to eq(@time_now)
end
Run Code Online (Sandbox Code Playgroud)

这样做时我收到以下错误:

 Failure/Error: Time.stub!(:now).and_return(@time_now)
 NoMethodError:
   undefined method `stub!' for Time:Class
Run Code Online (Sandbox Code Playgroud)

知道为什么会发生这种情况吗?

ruby rspec

21
推荐指数
4
解决办法
1万
查看次数

标签 统计

rspec ×1

ruby ×1