[rspec-users] Why does this stub doesn't work?

Zach Dennis zach.dennis at gmail.com
Sun Oct 24 17:04:37 EDT 2010


On Sun, Oct 24, 2010 at 11:21 AM, Haim Ashkenazi
<haim.ashkenazi at gmail.com>wrote:

> Hi
>
> I wonder why this stub doesn't work:
>
> # ruby 1.8.7, rspec 2.0.1
> require 'rubygems'
> require 'rspec'
>
> Rspec.configure do |c|
>   c.mock_with :rspec
> end
>
> class SayHello
>   def say_hello
>     "hello"
>   end
> end
>
> describe "test string" do
>   it "should interpret stub correctly" do
>     SayHello.stub!(:say_hello).and_return('NO')
>     sh = SayHello.new()
>     sh.say_hello.should eql('NO')
>   end
> end
>
> In your example you are stubbing a class method. In your implementation you
have defined an instance method. To have this work for your given
implementation you need to know about the instance you are working with, ie:

it "should interpret stub correctly" do
    sh = SayHello.new()
    sh.stub!(:say_hello).and_return 'NO'
    sh.say_hello.should eql('NO')
 end

Hope this helps,

Zach


>
> The result is:
>
> tryouts ➤ rspec -f n test_spec.rb
>
> test string
>   should interpret stub correctly (FAILED - 1)
>
> Failures:
>   1) test string should interpret stub correctly
>      Failure/Error: sh.say_hello.should eql('NO')
>
>      expected "NO"
>           got "hello"
>
>      (compared using eql?)
>      # ./test_spec.rb:18
>
> Finished in 0.0016 seconds
> 1 example, 1 failure
>
> Any ideas?
>
> Bye
>
> Haim Ashkenazi
>
>
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>



-- 
Zach Dennis
http://www.continuousthinking.com (personal)
http://www.mutuallyhuman.com (hire me)
http://ideafoundry.info/behavior-driven-development (first rate BDD
training)
@zachdennis (twitter)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101024/0b3bbd14/attachment.html>


More information about the rspec-users mailing list