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 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101024/369fda1b/attachment-0001.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: PGP.sig Type: application/pgp-signature Size: 194 bytes Desc: This is a digitally signed message part URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101024/369fda1b/attachment-0001.bin>
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 youhave 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>
Hi Zach, On Oct 24, 2010, at 11:04 PM, Zach Dennis wrote:> > > 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,Thanks for your help. I''ve found in the archives that you have to use mocha to do these kind of things. 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. I''m probably doing something wrong so I''ll upload it to github and ask this list again for help. Thanks. Bye> > 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) > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101025/b2fbc7d7/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: PGP.sig Type: application/pgp-signature Size: 194 bytes Desc: This is a digitally signed message part URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101025/b2fbc7d7/attachment.bin>
On Oct 25, 2010, at 3:20 AM, Haim Ashkenazi wrote:> Hi Zach, > > On Oct 24, 2010, at 11:04 PM, Zach Dennis wrote: > >> >> >> 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, > > 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?> I''m probably doing something wrong so I''ll upload it to github and ask this list again for help. > > Thanks. > > Bye > >> >> 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) >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101025/56a11ab3/attachment-0001.html>
Hi David, On Oct 25, 2010, at 11:23 AM, David Chelimsky wrote:> On Oct 25, 2010, at 3:20 AM, Haim Ashkenazi wrote: > >> Hi Zach, >> >> On Oct 24, 2010, at 11:04 PM, Zach Dennis wrote: >> >>> >>> >>> 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, >> >> 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.Sorry, I didn''t mean it this way. I actually tried it with RR and mocha and for some reason it didn''t work for me (see next comment).> >> 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?The problem I''m trying to solve is a little more complicated then the example. I''ve sent a new email with a better description of the problem here: http://rubyforge.org/pipermail/rspec-users/2010-October/018571.html Somehow the links are not displayed (I''ve sent a follow-up message but it''s not in the archive yet), so here they are: The repository is here: http://github.com/babysnakes/runssh The spec with the problem is http://github.com/babysnakes/runssh/blob/5197b763f391d6d358ca7bc5838375c9247271d8/spec/runsshlib/cli_spec.rb (line 127). The problem is that I have a SshBackend class which initializes with arguments. It also has a shell method which invokes exec. The SshBackend is invoked from the CLI class. I was trying to make sure that :new was called with the correct arguments and that :shellI is executed. One way would be to mock the :shell method (this email was about that) so that :exec would not be called and the other was to mock the :new method and return a mocked object (my second email). Somehow, non of these works for me. Thanks in advance Haim -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101025/e6488874/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: PGP.sig Type: application/pgp-signature Size: 194 bytes Desc: This is a digitally signed message part URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101025/e6488874/attachment.bin>