Experimenting with stub in irb with RSpec2

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:[email protected]/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:inrequire’
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,

Sarah
http://www.ultrasaurus.com
http://blazingcloud.net

On Nov 15, 2010, at 6:43 PM, Sarah A. wrote:

ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]

Time.now
=> 20

Time.now
=> 20

I can’t figure out how to do that in RSpec 2. Here’s what I’ve tried:

Here’s how you have to do it right now:

require ‘rspec/mocks’
require ‘rspec/mocks/extensions/object’
Time.stub(:now).and_return(10,20)
Time.now
Time.now
Time.now

The reason you have to require ‘rspec/mocks/extensions/object’ is to
make sure that requiring ‘rspec/mocks’ doesn’t automatically add stub
and should_receive to every object. That’s so rspec-mocks can be used
outside RSpec, and is a requirement of this feature:

http://relishapp.com/rspec/rspec-mocks/v/2-1/dir/outside-rspec/configure-any-test-framework-to-use-rspec-mocks

I’m pretty sure nobody is using this feature, and may be willing to
reconsider, but I need to think about it. For now, go ahead and require
‘rspec/mocks/extensions/object’ and at least you can do the demo you’re
trying to do.

Cheers,
David

For now, go ahead and require
‘rspec/mocks/extensions/object’ and at least you can do the demo you’re
trying to do.

Yay! This works for me and I’m excited to be able to show it in my
class today.

Thanks so much for your quick response!

Sarah

UPDATE

This is now achieved with:

require ‘rspec/mocks/standalone’

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs