Borja Martín
2007-Oct-08 18:05 UTC
[rspec-users] stub actions that depend on the parameter
Hi, I''m pretty new to all related to bdd and rspec and I have the following question. Is it possible to stub actions that return different objects depending on the parameteres they were called with? Something like this: MyClass.stub!(:method).with(1).and_return(@mock_object_1) MyClass.stub!(:method).with(2).and_return(@mock_object_2) I know I could use MyClass.stub!(:method).and_return(@mock_object_1, @mock_object_2) so the first time the :method method is called, it would return the first object and then the second one, but the problem is that I cannot guarantee the order it will be called inside the model. Thanks in advance -- /** * dagi3d v4 | http://dagi3d.net */
David Chelimsky
2007-Oct-08 18:08 UTC
[rspec-users] stub actions that depend on the parameter
On 10/8/07, Borja Mart?n <borjam at dagi3d.net> wrote:> Hi, > I''m pretty new to all related to bdd and rspec and I have the following > question. Is it possible to stub actions that return different objects > depending on the parameteres they were called with?Something like this: > > MyClass.stub!(:method).with(1).and_return(@mock_object_1) > MyClass.stub!(:method).with(2).and_return(@mock_object_2)What happened when you tried this?> > I know I could use MyClass.stub!(:method).and_return(@mock_object_1, > @mock_object_2) so the first time the :method method is called, it would > return the first object and then the second one, but the problem is that > I cannot guarantee the order it will be called inside the model. > > Thanks in advance > > -- > /** > * dagi3d v4 | http://dagi3d.net > */ > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Borja Martín
2007-Oct-08 18:17 UTC
[rspec-users] stub actions that depend on the parameter
David Chelimsky escribi?:> On 10/8/07, Borja Mart?n <borjam at dagi3d.net> wrote: > >> Hi, >> I''m pretty new to all related to bdd and rspec and I have the following >> question. Is it possible to stub actions that return different objects >> depending on the parameteres they were called with?Something like this: >> >> MyClass.stub!(:method).with(1).and_return(@mock_object_1) >> MyClass.stub!(:method).with(2).and_return(@mock_object_2) >> > > What happened when you tried this? > >I become an error like this: ... undefined method `with'' for #<Spec::Mocks::MethodStub:0xb6f20858>>> I know I could use MyClass.stub!(:method).and_return(@mock_object_1, >> @mock_object_2) so the first time the :method method is called, it would >> return the first object and then the second one, but the problem is that >> I cannot guarantee the order it will be called inside the model. >> >> Thanks in advance >> >> -- >> /** >> * dagi3d v4 | http://dagi3d.net >> */ >> >> _______________________________________________ >> 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 >-- /** * dagi3d v4 | http://dagi3d.net */
David Chelimsky
2007-Oct-08 18:23 UTC
[rspec-users] stub actions that depend on the parameter
On 10/8/07, Borja Mart?n <borjam at dagi3d.net> wrote:> > David Chelimsky escribi?: > > On 10/8/07, Borja Mart?n <borjam at dagi3d.net> wrote: > > > >> Hi, > >> I''m pretty new to all related to bdd and rspec and I have the following > >> question. Is it possible to stub actions that return different objects > >> depending on the parameteres they were called with?Something like this: > >> > >> MyClass.stub!(:method).with(1).and_return(@mock_object_1) > >> MyClass.stub!(:method).with(2).and_return(@mock_object_2) > >> > > > > What happened when you tried this? > > > > > I become an error like this: > ... > undefined method `with'' for #<Spec::Mocks::MethodStub:0xb6f20858>OK - that makes sense - you''re probably using the latest release rather than the trunk. The trunk has been modified to support what you''re trying to do. In the mean time, you could use should_receive to get the same effect. It won''t care about order, but it will care that the message is received at one point. That help?> >> I know I could use MyClass.stub!(:method).and_return(@mock_object_1, > >> @mock_object_2) so the first time the :method method is called, it would > >> return the first object and then the second one, but the problem is that > >> I cannot guarantee the order it will be called inside the model. > >> > >> Thanks in advance > >> > >> -- > >> /** > >> * dagi3d v4 | http://dagi3d.net > >> */ > >> > >> _______________________________________________ > >> 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 > > > > > -- > /** > * dagi3d v4 | http://dagi3d.net > */ > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Borja Martín
2007-Oct-08 18:51 UTC
[rspec-users] stub actions that depend on the parameter
Hi, I''ll give a try to the trunk version. Thanks for such a quick reply. Regards. David Chelimsky escribi?:> On 10/8/07, Borja Mart?n <borjam at dagi3d.net> wrote: > >> David Chelimsky escribi?: >> >>> On 10/8/07, Borja Mart?n <borjam at dagi3d.net> wrote: >>> >>> >>>> Hi, >>>> I''m pretty new to all related to bdd and rspec and I have the following >>>> question. Is it possible to stub actions that return different objects >>>> depending on the parameteres they were called with?Something like this: >>>> >>>> MyClass.stub!(:method).with(1).and_return(@mock_object_1) >>>> MyClass.stub!(:method).with(2).and_return(@mock_object_2) >>>> >>>> >>> What happened when you tried this? >>> >>> >>> >> I become an error like this: >> ... >> undefined method `with'' for #<Spec::Mocks::MethodStub:0xb6f20858> >> > > OK - that makes sense - you''re probably using the latest release > rather than the trunk. The trunk has been modified to support what > you''re trying to do. > > In the mean time, you could use should_receive to get the same effect. > It won''t care about order, but it will care that the message is > received at one point. > > That help? > > >>>> I know I could use MyClass.stub!(:method).and_return(@mock_object_1, >>>> @mock_object_2) so the first time the :method method is called, it would >>>> return the first object and then the second one, but the problem is that >>>> I cannot guarantee the order it will be called inside the model. >>>> >>>> Thanks in advance >>>> >>>> -- >>>> /** >>>> * dagi3d v4 | http://dagi3d.net >>>> */ >>>> >>>> _______________________________________________ >>>> 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 >>> >>> >> -- >> /** >> * dagi3d v4 | http://dagi3d.net >> */ >> >> _______________________________________________ >> 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 >-- /** * dagi3d v4 | http://dagi3d.net */
Scott Taylor
2007-Oct-08 19:35 UTC
[rspec-users] stub actions that depend on the parameter
On Oct 8, 2007, at 2:23 PM, David Chelimsky wrote:> On 10/8/07, Borja Mart?n <borjam at dagi3d.net> wrote: >> >> David Chelimsky escribi?: >>> On 10/8/07, Borja Mart?n <borjam at dagi3d.net> wrote: >>> >>>> Hi, >>>> I''m pretty new to all related to bdd and rspec and I have the >>>> following >>>> question. Is it possible to stub actions that return different >>>> objects >>>> depending on the parameteres they were called with?Something >>>> like this: >>>> >>>> MyClass.stub!(:method).with(1).and_return(@mock_object_1) >>>> MyClass.stub!(:method).with(2).and_return(@mock_object_2) >>>> >>> >>> What happened when you tried this? >>> >>> >> I become an error like this: >> ... >> undefined method `with'' for #<Spec::Mocks::MethodStub:0xb6f20858> > > OK - that makes sense - you''re probably using the latest release > rather than the trunk. The trunk has been modified to support what > you''re trying to do.What is the syntax for that? Scott
David Chelimsky
2007-Oct-08 19:40 UTC
[rspec-users] stub actions that depend on the parameter
On 10/8/07, Scott Taylor <mailing_lists at railsnewbie.com> wrote:> > On Oct 8, 2007, at 2:23 PM, David Chelimsky wrote: > > > On 10/8/07, Borja Mart?n <borjam at dagi3d.net> wrote: > >> > >> David Chelimsky escribi?: > >>> On 10/8/07, Borja Mart?n <borjam at dagi3d.net> wrote: > >>> > >>>> Hi, > >>>> I''m pretty new to all related to bdd and rspec and I have the > >>>> following > >>>> question. Is it possible to stub actions that return different > >>>> objects > >>>> depending on the parameteres they were called with?Something > >>>> like this: > >>>> > >>>> MyClass.stub!(:method).with(1).and_return(@mock_object_1) > >>>> MyClass.stub!(:method).with(2).and_return(@mock_object_2) > >>>> > >>> > >>> What happened when you tried this? > >>> > >>> > >> I become an error like this: > >> ... > >> undefined method `with'' for #<Spec::Mocks::MethodStub:0xb6f20858> > > > > OK - that makes sense - you''re probably using the latest release > > rather than the trunk. The trunk has been modified to support what > > you''re trying to do. > > What is the syntax for that?Just like Borja suggested: MyClass.stub!(:method).with(2).and_return(@mock_object_2) This is tricky though - it does NOT pass through other requests - simply ignores them. So if you do this, you must stub! every possible set of args.> > Scott > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >