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

> 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:

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


> 
> 
> 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,
> 
> Sarah
> http://www.ultrasaurus.com
> http://blazingcloud.net
> 
> -- 
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to