On Wed, Jun 17, 2009 at 10:14 AM, Andrew Premdas<apremdas at gmail.com> wrote:> Please have a look at > http://gist.github.com/131277 > What I''d like to do is create a shared_examples group which I can parametize > a method. So my shared example would perhaps use method named_address=, or > set_named_address and then groups that behave like "Named address" would be > able to override this method so it sets the correct address e.g. the last > billing addressHere''s the short version: There''s been a lot of discussion on this list about parameterizing shared examples. Basically, given the current design it can''t happen, and macros are a perfectly good solution to the problem, so there''s not much incentive to change the design. I''ll try to follow up later with a recommendation about a macro, unless somebody else beats me to it :) Cheers, David> TIA > Andrew > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On 17 Jun 2009, at 16:21, David Chelimsky wrote:> On Wed, Jun 17, 2009 at 10:14 AM, Andrew Premdas<apremdas at gmail.com> > wrote: >> Please have a look at >> http://gist.github.com/131277 >> What I''d like to do is create a shared_examples group which I can >> parametize >> a method. So my shared example would perhaps use method >> named_address=, or >> set_named_address and then groups that behave like "Named address" >> would be >> able to override this method so it sets the correct address e.g. >> the last >> billing address > > Here''s the short version: There''s been a lot of discussion on this > list about parameterizing shared examples. Basically, given the > current design it can''t happen, and macros are a perfectly good > solution to the problem, so there''s not much incentive to change the > design.You can use instance variables to pass values to the shared examples, though, right? cheers, Matt Wynne http://mattwynne.net +447974 430184
On Wed, Jun 17, 2009 at 1:19 PM, Matt Wynne<matt at mattwynne.net> wrote:> > On 17 Jun 2009, at 16:21, David Chelimsky wrote: > >> On Wed, Jun 17, 2009 at 10:14 AM, Andrew Premdas<apremdas at gmail.com> >> wrote: >>> >>> Please have a look at >>> http://gist.github.com/131277 >>> What I''d like to do is create a shared_examples group which I can >>> parametize >>> a method. So my shared example would perhaps use method named_address=, >>> or >>> set_named_address and then groups that behave like "Named address" would >>> be >>> able to override this method so it sets the correct address e.g. the last >>> billing address >> >> Here''s the short version: There''s been a lot of discussion on this >> list about parameterizing shared examples. Basically, given the >> current design it can''t happen, and macros are a perfectly good >> solution to the problem, so there''s not much incentive to change the >> design. > > You can use instance variables to pass values to the shared examples, > though, right?At your own risk, yes, of course :)> > cheers, > Matt Wynne > > http://mattwynne.net > +447974 430184 > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On Wed, Jun 17, 2009 at 11:14 AM, Andrew Premdas<apremdas at gmail.com> wrote:> What I''d like to do is create a shared_examples group which I can parametize > a method. So my shared example would perhaps use method named_address=, or > set_named_address and then groups that behave like "Named address" would be > able to override this method so it sets the correct address e.g. the last > billing addressThis is horrendously ugly, but I was able to achieve something like what you''re talking about using a combination of instance variables and ''eval'' statements in the shared behavior. See: http://gist.github.com/131526 Readable and elegant? Not really. But I have to set up a crapload of these classes layered on top of somebody else''s SOAP API, and spec completeness was more important to me than spec maintainability. (Also, it didn''t occur to me to try macros.) >8-> -- Have Fun, Steve Eley (sfeley at gmail.com) ESCAPE POD - The Science Fiction Podcast Magazine http://www.escapepod.org
David Chelimsky wrote:> On Wed, Jun 17, 2009 at 1:19 PM, Matt Wynne<matt at mattwynne.net> wrote: > >> On 17 Jun 2009, at 16:21, David Chelimsky wrote: >> >> >>> On Wed, Jun 17, 2009 at 10:14 AM, Andrew Premdas<apremdas at gmail.com> >>> wrote: >>> >>>> Please have a look at >>>> http://gist.github.com/131277 >>>> What I''d like to do is create a shared_examples group which I can >>>> parametize >>>> a method. So my shared example would perhaps use method named_address=, >>>> or >>>> set_named_address and then groups that behave like "Named address" would >>>> be >>>> able to override this method so it sets the correct address e.g. the last >>>> billing address >>>> >>> Here''s the short version: There''s been a lot of discussion on this >>> list about parameterizing shared examples. Basically, given the >>> current design it can''t happen, and macros are a perfectly good >>> solution to the problem, so there''s not much incentive to change the >>> design. >>> >> You can use instance variables to pass values to the shared examples, >> though, right? >> > > At your own risk, yes, of course :) >If you do go down that route, I recommend method calls instead of instance variables. That way it will yell out you when you forget to define one. :) -Ben> >> cheers, >> Matt Wynne >> >> http://mattwynne.net >> +447974 430184 >> >> _______________________________________________ >> 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 Thu, Jun 18, 2009 at 12:17 AM, Ben Mabey<ben at benmabey.com> wrote:> > If you do go down that route, I recommend method calls instead of instance > variables. ?That way it will yell out you when you forget to define one. :)Hey, instance variables do that too. Yelling == "my tests fail." >8-> (And if they don''t, I''m *really* doing something wrong...) -- Have Fun, Steve Eley (sfeley at gmail.com) ESCAPE POD - The Science Fiction Podcast Magazine http://www.escapepod.org
On Thu, Jun 18, 2009 at 1:36 AM, Stephen Eley<sfeley at gmail.com> wrote:> On Thu, Jun 18, 2009 at 12:17 AM, Ben Mabey<ben at benmabey.com> wrote: >> >> If you do go down that route, I recommend method calls instead of instance >> variables. ?That way it will yell out you when you forget to define one. :) > > Hey, instance variables do that too. ?Yelling == "my tests fail." ?>8-> > > (And if they don''t, I''m *really* doing something wrong...)But methods are more explicit. Suppose you want to parametrize on the value of a certain property named "bar". Not defining @bar will raise an NoMethodError because you called something on nil. Not defining the method will raise a NoMethodError because bar isn''t defined, which tells you immediately what you need to solve. You could also do something like def bar raise NotImplementedError, "please define this method in each of your example groups that use this shared behavior" end and it will be even more explicit. *But* this means you have to define the method *after* you include the shared examples, or it will fail (which is a code smell, IMO). -foca> > > -- > Have Fun, > ? Steve Eley (sfeley at gmail.com) > ? ESCAPE POD - The Science Fiction Podcast Magazine > ? http://www.escapepod.org > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
David Chelimsky wrote:> On Wed, Jun 17, 2009 at 10:14 AM, Andrew Premdas<apremdas at gmail.com> wrote: > >> Please have a look at >> http://gist.github.com/131277 >> What I''d like to do is create a shared_examples group which I can parametize >> a method. So my shared example would perhaps use method named_address=, or >> set_named_address and then groups that behave like "Named address" would be >> able to override this method so it sets the correct address e.g. the last >> billing address >> > > Here''s the short version: There''s been a lot of discussion on this > list about parameterizing shared examples. Basically, given the > current design it can''t happen, and macros are a perfectly good > solution to the problem, so there''s not much incentive to change the > design. > > I''ll try to follow up later with a recommendation about a macro, > unless somebody else beats me to it :) >I beat David to it. :) Here are some options: http://gist.github.com/131701 I think I like #7 and #8 the best with the eval and #subject use. With #8 I intentionally modify the backtrace so that the when errors happen it points to where the macro was called, not defined. Sometimes this is desirable, YMMV. FWIW, I question the value of such specs... but that is the macro approach. -Ben