In class, to give people a feel for how stubs work, I used to do this
with rspec 1.x:
sarah:~$ gem list rspec
*** LOCAL GEMS ***
rspec (1.3.0)
rspec-rails (1.3.2)
sarah:~$ ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
sarah:~$ irb
require ‘spec’
=> true
require ‘spec/mocks’
=> true
Time.stub!(:now).and_return(10,20)
=>
#Proc:0x00000001015936f0@/Library/Ruby/Gems/1.8/gems/rspec-1.3.0/lib/spec/mocks/message_expectation.rb:61
Time.now
=> 10
Time.now
=> 20
Time.now
=> 20
I can’t figure out how to do that in RSpec 2. Here’s what I’ve tried:
sarah:~$ gem list rspec
*** LOCAL GEMS ***
rspec (2.0.1, 2.0.0, 2.0.0.beta.22)
rspec-core (2.0.1, 2.0.0, 2.0.0.beta.22)
rspec-expectations (2.0.1, 2.0.0, 2.0.0.beta.22)
rspec-mocks (2.0.1, 2.0.0, 2.0.0.beta.22)
rspec-rails (2.0.1, 2.0.0, 2.0.0.beta.22)
sarah:~$ ruby -v
ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin10.4.0]
sarah:~$ irb
ruby-1.8.7-p302 > require ‘rspec’
=> true
ruby-1.8.7-p302 > require ‘rspec/mocks’
=> false
ruby-1.8.7-p302 > require ‘rspec-mocks’
LoadError: no such file to load – rspec-mocks
from
/Users/sarah/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
gem_original_require' from /Users/sarah/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
require’
from (irb):4
ruby-1.8.7-p302 > Time.stub!(:now).and_return(10,20)
NoMethodError: undefined method `stub!’ for Time:Class
from (irb):5
Can anyone give me a hint?
Thanks in advance,