Hello, Just decided to check whether I am doing something that makes sense or not. I was thinking about how cool would it be to re-use examples (just like we reuse story scenarios with GivenScenario). I was not sure if this possibility already exists in rspec (and, honestly, was lazy to check), so I have created this helper: def given_it(name) example_definition = behaviour.example_definitions.find{|i| i.description == name } instance_eval(&example_definition.example_block) end so it is possible to write things like it "should do something after another action" do given_it "should successfully do another action" do_something.should be_fine end Is there anything already in rspec that allows me to do the same stuff? Or was it a bad idea at all? Yurii.
Have you seen shared behaviours? On 9/21/07, Yurii Rashkovskii <yrashk at gmail.com> wrote:> Hello, > > Just decided to check whether I am doing something that makes sense > or not. I was thinking about how cool would it be to re-use examples > (just like we reuse story scenarios with GivenScenario). I was not > sure if this possibility already exists in rspec (and, honestly, was > lazy to check), so I have created this helper: > > def given_it(name) > example_definition = behaviour.example_definitions.find{|i| > i.description == name } > instance_eval(&example_definition.example_block) > end > > so it is possible to write things like > > it "should do something after another action" do > given_it "should successfully do another action" > do_something.should be_fine > end > > Is there anything already in rspec that allows me to do the same > stuff? Or was it a bad idea at all? > > Yurii. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Sure! But they are not the same stuff. What I was looking for is it "should do #1" do ... end it "should do X after #1" do given_it "should do #1" should do_x end it "should do Y after #1" do given_it "should do #1" should do_y end it "should do Z after #1 Y" do given_it "should do Y after #1" should do_z end Or am I missing somethiing? On Sep 21, 2007, at 10:49 PM, David Chelimsky wrote:> Have you seen shared behaviours? > > On 9/21/07, Yurii Rashkovskii <yrashk at gmail.com> wrote: >> Hello, >> >> Just decided to check whether I am doing something that makes sense >> or not. I was thinking about how cool would it be to re-use examples >> (just like we reuse story scenarios with GivenScenario). I was not >> sure if this possibility already exists in rspec (and, honestly, was >> lazy to check), so I have created this helper: >> >> def given_it(name) >> example_definition = behaviour.example_definitions.find{|i| >> i.description == name } >> instance_eval(&example_definition.example_block) >> end >> >> so it is possible to write things like >> >> it "should do something after another action" do >> given_it "should successfully do another action" >> do_something.should be_fine >> end >> >> Is there anything already in rspec that allows me to do the same >> stuff? Or was it a bad idea at all? >> >> Yurii. >> _______________________________________________ >> 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
On 9/21/07, Yurii Rashkovskii <yrashk at gmail.com> wrote:> Sure! But they are not the same stuff. What I was looking for is > > it "should do #1" do > ... > end > it "should do X after #1" do > given_it "should do #1" > should do_x > end > it "should do Y after #1" do > given_it "should do #1" > should do_y > end > it "should do Z after #1 Y" do > given_it "should do Y after #1" > should do_z > endSorry mate - that just seems like endless confusion - the examples should never rely on each other that way - different animal from scenarios that involve steps.> > Or am I missing somethiing? > > > On Sep 21, 2007, at 10:49 PM, David Chelimsky wrote: > > > Have you seen shared behaviours? > > > > On 9/21/07, Yurii Rashkovskii <yrashk at gmail.com> wrote: > >> Hello, > >> > >> Just decided to check whether I am doing something that makes sense > >> or not. I was thinking about how cool would it be to re-use examples > >> (just like we reuse story scenarios with GivenScenario). I was not > >> sure if this possibility already exists in rspec (and, honestly, was > >> lazy to check), so I have created this helper: > >> > >> def given_it(name) > >> example_definition = behaviour.example_definitions.find{|i| > >> i.description == name } > >> instance_eval(&example_definition.example_block) > >> end > >> > >> so it is possible to write things like > >> > >> it "should do something after another action" do > >> given_it "should successfully do another action" > >> do_something.should be_fine > >> end > >> > >> Is there anything already in rspec that allows me to do the same > >> stuff? Or was it a bad idea at all? > >> > >> Yurii. > >> _______________________________________________ > >> 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 > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
This concept violates oo if you want to test whether or not r method calls another method you would do that in the test and label it occordingly. What you are defining is such a narrow use case I think it would be abused Sent via BlackBerry from T-Mobile -----Original Message----- From: "David Chelimsky" <dchelimsky at gmail.com> Date: Fri, 21 Sep 2007 22:03:30 To:rspec-users <rspec-users at rubyforge.org> Subject: Re: [rspec-users] given_it On 9/21/07, Yurii Rashkovskii <yrashk at gmail.com> wrote:> Sure! But they are not the same stuff. What I was looking for is > > it "should do #1" do > ... > end > it "should do X after #1" do > given_it "should do #1" > should do_x > end > it "should do Y after #1" do > given_it "should do #1" > should do_y > end > it "should do Z after #1 Y" do > given_it "should do Y after #1" > should do_z > endSorry mate - that just seems like endless confusion - the examples should never rely on each other that way - different animal from scenarios that involve steps.> > Or am I missing somethiing? > > > On Sep 21, 2007, at 10:49 PM, David Chelimsky wrote: > > > Have you seen shared behaviours? > > > > On 9/21/07, Yurii Rashkovskii <yrashk at gmail.com> wrote: > >> Hello, > >> > >> Just decided to check whether I am doing something that makes sense > >> or not. I was thinking about how cool would it be to re-use examples > >> (just like we reuse story scenarios with GivenScenario). I was not > >> sure if this possibility already exists in rspec (and, honestly, was > >> lazy to check), so I have created this helper: > >> > >> def given_it(name) > >> example_definition = behaviour.example_definitions.find{|i| > >> i.description == name } > >> instance_eval(&example_definition.example_block) > >> end > >> > >> so it is possible to write things like > >> > >> it "should do something after another action" do > >> given_it "should successfully do another action" > >> do_something.should be_fine > >> end > >> > >> Is there anything already in rspec that allows me to do the same > >> stuff? Or was it a bad idea at all? > >> > >> Yurii. > >> _______________________________________________ > >> 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 > > _______________________________________________ > 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
>> Sure! But they are not the same stuff. What I was looking for is >> >> it "should do #1" do >> ... >> end >> it "should do X after #1" do >> given_it "should do #1" >> should do_x >> end >> it "should do Y after #1" do >> given_it "should do #1" >> should do_y >> end >> it "should do Z after #1 Y" do >> given_it "should do Y after #1" >> should do_z >> end > > Sorry mate - that just seems like endless confusion - the examples > should never rely on each other that way - different animal from > scenarios that involve steps.I agree that they should not rely on each other _indirectly_. But why they should not _reuse_ each other? I find it very useful for testing, say, different aspects of, say, controller''s behavior. I simply do not want to put all checks into one example, like it should do this, this and this and in result we should have this, this and that. How do you propose to test different aspects of single actions in a nice way?
If you want to reuse stuff in your tests put those test methods in a separate module and include them into you specs Sent via BlackBerry from T-Mobile -----Original Message----- From: Yurii Rashkovskii <yrashk at gmail.com> Date: Fri, 21 Sep 2007 23:12:50 To:rspec-users <rspec-users at rubyforge.org> Subject: Re: [rspec-users] given_it>> Sure! But they are not the same stuff. What I was looking for is >> >> it "should do #1" do >> ... >> end >> it "should do X after #1" do >> given_it "should do #1" >> should do_x >> end >> it "should do Y after #1" do >> given_it "should do #1" >> should do_y >> end >> it "should do Z after #1 Y" do >> given_it "should do Y after #1" >> should do_z >> end > > Sorry mate - that just seems like endless confusion - the examples > should never rely on each other that way - different animal from > scenarios that involve steps.I agree that they should not rely on each other _indirectly_. But why they should not _reuse_ each other? I find it very useful for testing, say, different aspects of, say, controller''s behavior. I simply do not want to put all checks into one example, like it should do this, this and this and in result we should have this, this and that. How do you propose to test different aspects of single actions in a nice way? _______________________________________________ rspec-users mailing list rspec-users at rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Then I will have tens of methods that in fact has nothing really different from reused examples On Sep 21, 2007, at 11:14 PM, lancecarlson at gmail.com wrote:> If you want to reuse stuff in your tests put those test methods in > a separate module and include them into you specs > Sent via BlackBerry from T-Mobile >
On 9/21/07, Yurii Rashkovskii <yrashk at gmail.com> wrote:> Then I will have tens of methods that in fact has nothing really > different from reused examplesBut, as methods, they are more clearly differentiated for reuse. The problem is that if I reuse an example, and then decide to change that example, I may accidentally change the meaning of other examples. This is possible w/ helper methods too, but I think it is less likely. You can keep pursuing this if you like, but I can tell you right now that it is very likely not going to happen. We already have a few different ways to deal w/ reuse and this one seems more confusing to me than helpful. FWIW, David> > On Sep 21, 2007, at 11:14 PM, lancecarlson at gmail.com wrote: > > > If you want to reuse stuff in your tests put those test methods in > > a separate module and include them into you specs > > Sent via BlackBerry from T-Mobile > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Ok, let me try to explain. I have some controllers that are doing some kind of "main" action and some additional ones. These actions are belong to different aspects of logic and thus I''d prefer to test them separately to have clean and nice code that can be easily modified. Then, what''s better to read? it "should do main action" do do_main_action end it "should set some value in session if there was no another specific value in session before" do session[:a] = 1 do_main_action session[:b].should == b end def do_main_action post :create, ... end or it "should do main action" do post :create, ... end it "should set some value in session if there was no another specific value in session before" do session[:a] = 1 given_it "should do main action" session[:b].should == b end I personally like second one. P.S. Examples above are surely artificial. On Sep 21, 2007, at 11:11 PM, lancecarlson at gmail.com wrote:> This concept violates oo if you want to test whether or not r > method calls another method you would do that in the test and label > it occordingly. What you are defining is such a narrow use case I > think it would be abused > Sent via BlackBerry from T-Mobile > > -----Original Message----- > From: "David Chelimsky" <dchelimsky at gmail.com> > > Date: Fri, 21 Sep 2007 22:03:30 > To:rspec-users <rspec-users at rubyforge.org> > Subject: Re: [rspec-users] given_it > > > On 9/21/07, Yurii Rashkovskii <yrashk at gmail.com> wrote: >> Sure! But they are not the same stuff. What I was looking for is >> >> it "should do #1" do >> ... >> end >> it "should do X after #1" do >> given_it "should do #1" >> should do_x >> end >> it "should do Y after #1" do >> given_it "should do #1" >> should do_y >> end >> it "should do Z after #1 Y" do >> given_it "should do Y after #1" >> should do_z >> end > > Sorry mate - that just seems like endless confusion - the examples > should never rely on each other that way - different animal from > scenarios that involve steps. > >> >> Or am I missing somethiing? >> >> >> On Sep 21, 2007, at 10:49 PM, David Chelimsky wrote: >> >>> Have you seen shared behaviours? >>> >>> On 9/21/07, Yurii Rashkovskii <yrashk at gmail.com> wrote: >>>> Hello, >>>> >>>> Just decided to check whether I am doing something that makes sense >>>> or not. I was thinking about how cool would it be to re-use >>>> examples >>>> (just like we reuse story scenarios with GivenScenario). I was not >>>> sure if this possibility already exists in rspec (and, honestly, >>>> was >>>> lazy to check), so I have created this helper: >>>> >>>> def given_it(name) >>>> example_definition = behaviour.example_definitions.find{|i| >>>> i.description == name } >>>> instance_eval(&example_definition.example_block) >>>> end >>>> >>>> so it is possible to write things like >>>> >>>> it "should do something after another action" do >>>> given_it "should successfully do another action" >>>> do_something.should be_fine >>>> end >>>> >>>> Is there anything already in rspec that allows me to do the same >>>> stuff? Or was it a bad idea at all? >>>> >>>> Yurii. >>>> _______________________________________________ >>>> 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 >> >> _______________________________________________ >> 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 > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
David, On Sep 21, 2007, at 11:19 PM, David Chelimsky wrote:> On 9/21/07, Yurii Rashkovskii <yrashk at gmail.com> wrote: >> Then I will have tens of methods that in fact has nothing really >> different from reused examples > > But, as methods, they are more clearly differentiated for reuse. > > The problem is that if I reuse an example, and then decide to change > that example, I may accidentally change the meaning of other examples. > This is possible w/ helper methods too, but I think it is less likely.I see no difference, really. You can break anything, either helper or example. But when using helpers, you''d likely to have worse code (just IMHO)> > You can keep pursuing this if you like, but I can tell you right now > that it is very likely not going to happen. We already have a few > different ways to deal w/ reuse and this one seems more confusing to > me than helpful. >I am not going to change anything, that''s simply not my intention. My intention was to check what people think about this approach. I just found it very convenient for my coding experience. I do not need spend time extracting helpers, I do not need to keep single line examples that call helpers. I just write down pieces of logic, that''s it. Anyway, thank you for your feedback. Yurii.
I have to agree with David. (It''s in the contract ;) Scenarios are reusable as steps in order to chain multiple scenarios together (to describe the various coarse-grained stages of a workflow, state machine or wizard, for example). At an example level it is just confusing. I''ve noticed that if I find myself wanting to do that, it''s an indication that the objects are too tightly coupled or dependent on one another. There is usually a cleaner abstraction in there waiting to be discovered that will mean you don''t need to chain examples in this way. fwiw GivenScenario was introduced into JBehave to work around the constraint that scenarios were only allowed to be of the form Given/When/Then in the early days. Then we relaxed scenarios to allow mixing up steps in any order (given.. when.. then.. when.. then..) which made them read much more naturally. GivenScenarios aren''t used much now other than in the specific situations I described above (state machines, etc). Cheers, Dan David Chelimsky wrote:> On 9/21/07, Yurii Rashkovskii <yrashk at gmail.com> wrote: > >> Sure! But they are not the same stuff. What I was looking for is >> >> it "should do #1" do >> ... >> end >> it "should do X after #1" do >> given_it "should do #1" >> should do_x >> end >> it "should do Y after #1" do >> given_it "should do #1" >> should do_y >> end >> it "should do Z after #1 Y" do >> given_it "should do Y after #1" >> should do_z >> end >> > > Sorry mate - that just seems like endless confusion - the examples > should never rely on each other that way - different animal from > scenarios that involve steps. > > >> Or am I missing somethiing? >> >> >> On Sep 21, 2007, at 10:49 PM, David Chelimsky wrote: >> >> >>> Have you seen shared behaviours? >>> >>> On 9/21/07, Yurii Rashkovskii <yrashk at gmail.com> wrote: >>> >>>> Hello, >>>> >>>> Just decided to check whether I am doing something that makes sense >>>> or not. I was thinking about how cool would it be to re-use examples >>>> (just like we reuse story scenarios with GivenScenario). I was not >>>> sure if this possibility already exists in rspec (and, honestly, was >>>> lazy to check), so I have created this helper: >>>> >>>> def given_it(name) >>>> example_definition = behaviour.example_definitions.find{|i| >>>> i.description == name } >>>> instance_eval(&example_definition.example_block) >>>> end >>>> >>>> so it is possible to write things like >>>> >>>> it "should do something after another action" do >>>> given_it "should successfully do another action" >>>> do_something.should be_fine >>>> end >>>> >>>> Is there anything already in rspec that allows me to do the same >>>> stuff? Or was it a bad idea at all? >>>> >>>> Yurii. >>>> _______________________________________________ >>>> 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 >>> >> _______________________________________________ >> 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/20070921/120f9874/attachment.html