Why does this stub doesn't work?


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

On Sun, Oct 24, 2010 at 11:21 AM, Haim Ashkenazi
[email protected]wrote:

end
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


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

On Oct 25, 2010, at 3:20 AM, Haim Ashkenazi wrote:

def say_hello
end

Thanks for your help. I’ve found in the archives that you have to use mocha to
do these kind of things.

This is incorrect. You are free to use mocha, but rspec-mocks, RR, and
flexmock are all perfectly capable of stubbing class methods, so you
don’t have to use mocha.

I tried a different approach (to mock the initializer) but although this works
on a simple setup, it didn’t work for me on my real classes.

What Zach suggested is the correct approach. What problem are you seeing
when you try it?


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

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