I am in the process of writing a simple DSL inside of rspec, which defines some methods that wrap describe/example and augment the metadata, like capybara''s "feature/scenario" methods. From the excellent RSpec book, I know how to use Configuration to extend RSpec in a forward-compatible way, but it is unclear to me how to define methods on the very top-level, so I could write specs like this: require ''my_class'' describify ''something funky'' do; ...; end I looked into how capybara is doing it, and I see that when one includes ''capybara/rspec'' that causes a class-level method to be defined in the "global" scope[1]. Is this the preferred way of defining such a method for use in RSpec DSLs, or is there a better way? I have been able to use this method successfully. Thanks, Sam 1. https://github.com/jnicklas/capybara/blob/f41018f884c53633b6744d26ed1d21fb08e3b92f/lib/capybara/rspec/features.rb -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20111101/f186a56b/attachment.html>
David Chelimsky
2011-Nov-01 15:19 UTC
[rspec-users] API for defining top-level DSL methods?
On Nov 1, 2011, at 9:34 AM, Sam Goldman wrote:> I am in the process of writing a simple DSL inside of rspec, which defines some methods that wrap describe/example and augment the metadata, like capybara''s "feature/scenario" methods. From the excellent RSpec book, I know how to use Configuration to extend RSpec in a forward-compatible way, but it is unclear to me how to define methods on the very top-level, so I could write specs like this: > > require ''my_class'' > describify ''something funky'' do; ...; end > > I looked into how capybara is doing it, and I see that when one includes ''capybara/rspec'' that causes a class-level method to be defined in the "global" scope[1]. Is this the preferred way of defining such a method for use in RSpec DSLs, or is there a better way? I have been able to use this method successfully. > > Thanks, > Sam > > 1. https://github.com/jnicklas/capybara/blob/f41018f884c53633b6744d26ed1d21fb08e3b92f/lib/capybara/rspec/features.rbWe don''t have a formal API, but we should: https://github.com/rspec/rspec-core/issues/493 In the mean time, just use alias. Cheers, David -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20111101/cc841881/attachment.html>