Lille
2010-Jul-23 23:46 UTC
[rspec-users] newbie: how to preserve NoMethodError under stubbing?
Hi, I''ve been browsing the RSpec book and the RDoc, but I can''t see how to ensure the following: Stub an instance with a method it doesn''t have and raise NoMethodError (or something like it.) I want to do this because I don''t want to use incorrect names for my dependency methods... Thanks, Lille
David Chelimsky
2010-Jul-24 06:26 UTC
[rspec-users] newbie: how to preserve NoMethodError under stubbing?
On Fri, Jul 23, 2010 at 6:46 PM, Lille <lille.penguini at gmail.com> wrote:> Hi, > > I''ve been browsing the RSpec book and the RDoc, but I can''t see how to > ensure the following: > > Stub an instance with a method it doesn''t have and raise NoMethodError > (or something like it.)RSpec doesn''t support anything like that. I''m not sure if any of the Ruby frameworks do, though I''ve been involved with conversations about this sort of thing before. I wouldn''t want behaviour like this myself unless it could be invoked explicitly with a command line argument, but was otherwise off. In other words, you could do something like: rspec spec --audit-stubbed-methods And then that would generate some sort of report. Feel free to submit a feature request to http://github.com/rspec/rspec-mocks if you''re interested in pursuing such. Cheers, David> > I want to do this because I don''t want to use incorrect names for my > dependency methods... > > Thanks, > > Lille > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Matt Wynne
2010-Jul-24 08:55 UTC
[rspec-users] newbie: how to preserve NoMethodError under stubbing?
Lille, On Sat, 24 Jul 2010 01:26 -0500, "David Chelimsky" <dchelimsky at gmail.com> wrote:> On Fri, Jul 23, 2010 at 6:46 PM, Lille <lille.penguini at gmail.com> wrote: > > Hi, > > > > I''ve been browsing the RSpec book and the RDoc, but I can''t see how to > > ensure the following: > > > > Stub an instance with a method it doesn''t have and raise NoMethodError > > (or something like it.) > > RSpec doesn''t support anything like that. I''m not sure if any of the > Ruby frameworks do, though I''ve been involved with conversations about > this sort of thing before. > > I wouldn''t want behaviour like this myself unless it could be invoked > explicitly with a command line argument, but was otherwise off. In > other words, you could do something like: > > rspec spec --audit-stubbed-methods > > And then that would generate some sort of report. > > Feel free to submit a feature request to > http://github.com/rspec/rspec-mocks if you''re interested in pursuing > such. > > Cheers, > DavidMost people ask for this kind of feature from their unit tests because they''re not combining mocking unit tests with end-to-end acceptance tests. Are you using both?> > > > > > I want to do this because I don''t want to use incorrect names for my > > dependency methods... > > > > Thanks, > > > > Lille > > _______________________________________________ > > 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 >
Wincent Colaiuta
2010-Jul-24 09:35 UTC
[rspec-users] newbie: how to preserve NoMethodError under stubbing?
El 24/07/2010, a las 08:26, David Chelimsky escribi?:> On Fri, Jul 23, 2010 at 6:46 PM, Lille <lille.penguini at gmail.com> wrote: >> Hi, >> >> I''ve been browsing the RSpec book and the RDoc, but I can''t see how to >> ensure the following: >> >> Stub an instance with a method it doesn''t have and raise NoMethodError >> (or something like it.) > > RSpec doesn''t support anything like that. I''m not sure if any of the > Ruby frameworks do, though I''ve been involved with conversations about > this sort of thing before.I''m not sure if I understand the request, but with RR you can do this:>> require ''rr''=> true>> extend RR::Adapters::RRMethods=> main>> foo = Object.new=> #<Object:0x101664bd8>>> stub(foo).bar { raise NoMethodError }=> #<RR::DoubleDefinitions::DoubleDefinition:0x10165ce60>>> foo.barNoMethodError: NoMethodError Although like I said, not sure if I understood Lille''s request. Cheers, Wincent
David Chelimsky
2010-Jul-24 11:15 UTC
[rspec-users] newbie: how to preserve NoMethodError under stubbing?
On Jul 24, 2010, at 4:35 AM, Wincent Colaiuta wrote:> El 24/07/2010, a las 08:26, David Chelimsky escribi?: > >> On Fri, Jul 23, 2010 at 6:46 PM, Lille <lille.penguini at gmail.com> wrote: >>> Hi, >>> >>> I''ve been browsing the RSpec book and the RDoc, but I can''t see how to >>> ensure the following: >>> >>> Stub an instance with a method it doesn''t have and raise NoMethodError >>> (or something like it.) >> >> RSpec doesn''t support anything like that. I''m not sure if any of the >> Ruby frameworks do, though I''ve been involved with conversations about >> this sort of thing before. > > I''m not sure if I understand the request, but with RR you can do this: > >>> require ''rr'' > => true >>> extend RR::Adapters::RRMethods > => main >>> foo = Object.new > => #<Object:0x101664bd8> >>> stub(foo).bar { raise NoMethodError } > => #<RR::DoubleDefinitions::DoubleDefinition:0x10165ce60> >>> foo.bar > NoMethodError: NoMethodError > > Although like I said, not sure if I understood Lille''s request.I read the request as this: class Foo def bar; end end f = Foo.new f.stub(:barr) #=> Error: "The Foo class does not have a ''barr'' method. Perhaps you meant to stub ''bar''" For me this would have very limited utility because 1/2 the time I''m deliberately stubbing methods that don''t even exist yet on doubles doubling for objects that don''t even exist yet, which is why I wouldn''t want it to be something that happens implicitly by default. Here''s another thread on the same matter from a little over a year ago: http://groups.google.com/group/rspec/browse_thread/thread/cf0b3eae192b9d8c/f5b77b008de628c7?lnk=gst&q=mock+audit#f5b77b008de628c7 Cheers, David> > Cheers, > Wincent
Lille
2010-Jul-24 13:34 UTC
[rspec-users] newbie: how to preserve NoMethodError under stubbing?
David, Yes, your reading of the request is what I originally meant. I appreciate the points made by others in this thread and the thread David has referred to. As I continue to learn RSpec I will undoubtedly avail myself of the approaches recommended above or in the linked thread, but I think an argument to stub() per the following could be useful: f.stub(:barr, :throw_no_such_method_error=>true) #=> Error: "The Foo class does not have a ''barr'' method. Perhaps you meant to stub ''bar''" Thanks, Lille On Jul 24, 7:15?am, David Chelimsky <dchelim... at gmail.com> wrote:> On Jul 24, 2010, at 4:35 AM, Wincent Colaiuta wrote: > > > > > > > El 24/07/2010, a las 08:26, David Chelimsky escribi?: > > >> On Fri, Jul 23, 2010 at 6:46 PM, Lille <lille.pengu... at gmail.com> wrote: > >>> Hi, > > >>> I''ve been browsing the RSpec book and the RDoc, but I can''t see how to > >>> ensure the following: > > >>> Stub an instance with a method it doesn''t have and raise NoMethodError > >>> (or something like it.) > > >> RSpec doesn''t support anything like that. I''m not sure if any of the > >> Ruby frameworks do, though I''ve been involved with conversations about > >> this sort of thing before. > > > I''m not sure if I understand the request, but with RR you can do this: > > >>> require ''rr'' > > => true > >>> extend RR::Adapters::RRMethods > > => main > >>> foo = Object.new > > => #<Object:0x101664bd8> > >>> stub(foo).bar { raise NoMethodError } > > => #<RR::DoubleDefinitions::DoubleDefinition:0x10165ce60> > >>> foo.bar > > NoMethodError: NoMethodError > > > Although like I said, not sure if I understood Lille''s request. > > I read the request as this: > > class Foo > ? def bar; end > end > > f = Foo.new > f.stub(:barr) > #=> Error: "The Foo class does not have a ''barr'' method. Perhaps you meant to stub ''bar''" > > For me this would have very limited utility because 1/2 the time I''m deliberately stubbing methods that don''t even exist yet on doubles doubling for objects that don''t even exist yet, which is why I wouldn''t want it to be something that happens implicitly by default. > > Here''s another thread on the same matter from a little over a year ago: > > http://groups.google.com/group/rspec/browse_thread/thread/cf0b3eae192... > > Cheers, > David > > > > > Cheers, > > Wincent > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
Wincent Colaiuta
2010-Jul-24 13:52 UTC
[rspec-users] newbie: how to preserve NoMethodError under stubbing?
El 24/07/2010, a las 15:34, Lille escribi?:> David, > > Yes, your reading of the request is what I originally meant. > > I appreciate the points made by others in this thread and the thread > David has referred to. > > As I continue to learn RSpec I will undoubtedly avail myself of the > approaches recommended above or in the linked thread, but I think an > argument to stub() per the following could be useful: > > f.stub(:barr, :throw_no_such_method_error=>true) > #=> Error: "The Foo class does not have a ''barr'' method. Perhaps you > meant to stub ''bar''"How about this: f.stub.existing(:bar) That''s probably RR influencing me there, which employs things like "stub.proxy" and so on. So maybe not such a good idea for rspec-mocks. Cheers, Wincent
Ashley Moran
2010-Jul-24 17:08 UTC
[rspec-users] newbie: how to preserve NoMethodError under stubbing?
On 24 Jul 2010, at 2:52 PM, Wincent Colaiuta wrote:> How about this: > > f.stub.existing(:bar) > > That''s probably RR influencing me there, which employs things like "stub.proxy" and so on. So maybe not such a good idea for rspec-mocks.Another option might be to deprecate #stub! as an alias for #stub and then bring it back as an equivalent to `stub.existing` above. Ash -- http://www.patchspace.co.uk/ http://www.linkedin.com/in/ashleymoran