Hey everyone! A few days ago, I tried my hand at creating a gem. I''ve been putting it off for a long time because of the lack of documentation (or the lack of my Googling skills). When I search how to create gems with RSpec, I get pages that talk about gem templates with the rspec option, as if that would solve it all. If there are no tutorials, maybe I could ask for your help in setting it up for a gem: Backgrounder I''m asking because it''s a bit frustrating doing things by trial and error. A lot of the problems I''ve come across were that the files that rspec-rails loads for me aren''t automatically available in the gem, so I have to manually include it. It became a problem when I had to test an ActiveRecord model, and worse when I had to test a controller. Currently, I''m stuck at the point where "get", "put", "post", "delete" aren''t available in the spec. I asked about it in StackOverflow and got the answer<http://stackoverflow.com/questions/2543885/why-dont-rspecs-methods-get-post-put-delete-work-in-a-controller-sp>, but it doesn''t explain what I have to do to basically replicate those methods. What did I do? As part of my trial and error process, I started including what rspec-rails includes http://github.com/dchelimsky/rspec-rails/blob/master/lib/spec/rails.rb, and the REST actions don''t seem to trigger an undefined error anymore, but now there''s another "uninitialized constant Rails". So.. before I go further, maybe I''m approaching this wrong since I''m having such a difficult time. What''s the best way to go about this? Is there a way to maybe include Rails and everything it has, for testing purposes? I know that you''re supposed to make gems framework agnostic but for brevity''s sake (and for my own sanity) I''m willing to make it very plugin-like for now. I''ve been reading and researching for 3 days you see :) Thanks, and I apologize for the novel! Ramon Tayag -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100330/df1c4fc8/attachment.html>
aslak hellesoy
2010-Mar-30 11:09 UTC
[rspec-users] Gem tutorials / setting up rspec in a gem
On Tue, Mar 30, 2010 at 12:17 PM, Ramon Tayag <ramon.tayag at gmail.com> wrote:> Hey everyone! > > A few days ago, I tried my hand at creating a gem. I''ve been putting it off > for a long time because of the lack of documentation (or the lack of my > Googling skills). When I search how to create gems with RSpec, I get pages > that talk about gem templates with the rspec option, as if that would solve > it all. > >Have you tried jeweler? Try this: gem install jeweler jeweler --help jeweler --rspec --summary "My awesome gem, which solves problems" --description "The awesome gem" awesome cd awesome rake -T rake spec # write a real spec rake version:write rake install See the Jeweler docs for more: http://wiki.github.com/technicalpickles/jeweler/ If there are no tutorials, maybe I could ask for your help in setting it up> for a gem: > > Backgrounder > I''m asking because it''s a bit frustrating doing things by trial and error. > A lot of the problems I''ve come across were that the files that rspec-rails > loads for me aren''t automatically available in the gem, so I have to > manually include it. It became a problem when I had to test an ActiveRecord > model, and worse when I had to test a controller. Currently, I''m stuck at > the point where "get", "put", "post", "delete" aren''t available in the spec. > I asked about it in StackOverflow and got the answer<http://stackoverflow.com/questions/2543885/why-dont-rspecs-methods-get-post-put-delete-work-in-a-controller-sp>, > but it doesn''t explain what I have to do to basically replicate those > methods. > > What did I do? > As part of my trial and error process, I started including what rspec-rails > includes > http://github.com/dchelimsky/rspec-rails/blob/master/lib/spec/rails.rb, > and the REST actions don''t seem to trigger an undefined error anymore, but > now there''s another "uninitialized constant Rails". So.. before I go > further, maybe I''m approaching this wrong since I''m having such a difficult > time. > > What''s the best way to go about this? Is there a way to maybe include Rails > and everything it has, for testing purposes? I know that you''re supposed to > make gems framework agnostic but for brevity''s sake (and for my own sanity) > I''m willing to make it very plugin-like for now. I''ve been reading and > researching for 3 days you see :) > >I''m not sure what your gem does, but if it has dependencies on Rails, the easiest way to develop it (in my experience) is to have a separate Rails app that _uses_ your gem, and that is not part of the gem''s source code. Then that Rails app could have specs (and maybe Cucumber features) that rely on the correct implementation of your gem. Maybe you could say a little more about what your gem does? Aslak> Thanks, and I apologize for the novel! > Ramon Tayag > > _______________________________________________ > 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/20100330/2313d7bc/attachment-0001.html>
Thanks for your reply aslak. Since then I''ve tried another approach - having a sample Rails application in the spec folder, much like shoulda<http://github.com/thoughtbot/shoulda/tree/master/test/rails_root/>. Still haven''t gotten it to work, but I''ll try anyway. If I have a separate Rails app (which sounds MUCH easier to do - thank you), won''t it be slow doing TDD because I''d have to publish my gem, update it in the Rails app, run tests, then back to the gem source code to make changes? I wasn''t too ambitious with my first gem. It allows subscriptions. The name "acts_as_subscribable" should be descriptive enough :) Ramon Tayag On Tue, Mar 30, 2010 at 7:09 PM, aslak hellesoy <aslak.hellesoy at gmail.com>wrote:> > > On Tue, Mar 30, 2010 at 12:17 PM, Ramon Tayag <ramon.tayag at gmail.com>wrote: > >> Hey everyone! >> >> A few days ago, I tried my hand at creating a gem. I''ve been putting it >> off for a long time because of the lack of documentation (or the lack of my >> Googling skills). When I search how to create gems with RSpec, I get pages >> that talk about gem templates with the rspec option, as if that would solve >> it all. >> >> > Have you tried jeweler? Try this: > > gem install jeweler > jeweler --help > jeweler --rspec --summary "My awesome gem, which solves problems" > --description "The awesome gem" awesome > cd awesome > rake -T > rake spec # write a real spec > rake version:write > rake install > > See the Jeweler docs for more: > http://wiki.github.com/technicalpickles/jeweler/ > > If there are no tutorials, maybe I could ask for your help in setting it up >> for a gem: >> >> Backgrounder >> I''m asking because it''s a bit frustrating doing things by trial and error. >> A lot of the problems I''ve come across were that the files that rspec-rails >> loads for me aren''t automatically available in the gem, so I have to >> manually include it. It became a problem when I had to test an ActiveRecord >> model, and worse when I had to test a controller. Currently, I''m stuck at >> the point where "get", "put", "post", "delete" aren''t available in the spec. >> I asked about it in StackOverflow and got the answer<http://stackoverflow.com/questions/2543885/why-dont-rspecs-methods-get-post-put-delete-work-in-a-controller-sp>, >> but it doesn''t explain what I have to do to basically replicate those >> methods. >> >> What did I do? >> As part of my trial and error process, I started including what >> rspec-rails includes >> http://github.com/dchelimsky/rspec-rails/blob/master/lib/spec/rails.rb, >> and the REST actions don''t seem to trigger an undefined error anymore, but >> now there''s another "uninitialized constant Rails". So.. before I go >> further, maybe I''m approaching this wrong since I''m having such a difficult >> time. >> >> What''s the best way to go about this? Is there a way to maybe include >> Rails and everything it has, for testing purposes? I know that you''re >> supposed to make gems framework agnostic but for brevity''s sake (and for my >> own sanity) I''m willing to make it very plugin-like for now. I''ve been >> reading and researching for 3 days you see :) >> >> > I''m not sure what your gem does, but if it has dependencies on Rails, the > easiest way to develop it (in my experience) is to have a separate Rails app > that _uses_ your gem, and that is not part of the gem''s source code. Then > that Rails app could have specs (and maybe Cucumber features) that rely on > the correct implementation of your gem. > > Maybe you could say a little more about what your gem does? > > Aslak > > >> Thanks, and I apologize for the novel! >> Ramon Tayag >> >> _______________________________________________ >> 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/20100330/cfc0bc43/attachment.html>
Oh and yes, I''m using jeweler. :) Ramon Tayag On Tue, Mar 30, 2010 at 7:09 PM, aslak hellesoy <aslak.hellesoy at gmail.com>wrote:> > > On Tue, Mar 30, 2010 at 12:17 PM, Ramon Tayag <ramon.tayag at gmail.com>wrote: > >> Hey everyone! >> >> A few days ago, I tried my hand at creating a gem. I''ve been putting it >> off for a long time because of the lack of documentation (or the lack of my >> Googling skills). When I search how to create gems with RSpec, I get pages >> that talk about gem templates with the rspec option, as if that would solve >> it all. >> >> > Have you tried jeweler? Try this: > > gem install jeweler > jeweler --help > jeweler --rspec --summary "My awesome gem, which solves problems" > --description "The awesome gem" awesome > cd awesome > rake -T > rake spec # write a real spec > rake version:write > rake install > > See the Jeweler docs for more: > http://wiki.github.com/technicalpickles/jeweler/ > > If there are no tutorials, maybe I could ask for your help in setting it up >> for a gem: >> >> Backgrounder >> I''m asking because it''s a bit frustrating doing things by trial and error. >> A lot of the problems I''ve come across were that the files that rspec-rails >> loads for me aren''t automatically available in the gem, so I have to >> manually include it. It became a problem when I had to test an ActiveRecord >> model, and worse when I had to test a controller. Currently, I''m stuck at >> the point where "get", "put", "post", "delete" aren''t available in the spec. >> I asked about it in StackOverflow and got the answer<http://stackoverflow.com/questions/2543885/why-dont-rspecs-methods-get-post-put-delete-work-in-a-controller-sp>, >> but it doesn''t explain what I have to do to basically replicate those >> methods. >> >> What did I do? >> As part of my trial and error process, I started including what >> rspec-rails includes >> http://github.com/dchelimsky/rspec-rails/blob/master/lib/spec/rails.rb, >> and the REST actions don''t seem to trigger an undefined error anymore, but >> now there''s another "uninitialized constant Rails". So.. before I go >> further, maybe I''m approaching this wrong since I''m having such a difficult >> time. >> >> What''s the best way to go about this? Is there a way to maybe include >> Rails and everything it has, for testing purposes? I know that you''re >> supposed to make gems framework agnostic but for brevity''s sake (and for my >> own sanity) I''m willing to make it very plugin-like for now. I''ve been >> reading and researching for 3 days you see :) >> >> > I''m not sure what your gem does, but if it has dependencies on Rails, the > easiest way to develop it (in my experience) is to have a separate Rails app > that _uses_ your gem, and that is not part of the gem''s source code. Then > that Rails app could have specs (and maybe Cucumber features) that rely on > the correct implementation of your gem. > > Maybe you could say a little more about what your gem does? > > Aslak > > >> Thanks, and I apologize for the novel! >> Ramon Tayag >> >> _______________________________________________ >> 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/20100330/d00c488c/attachment.html>
Hi Ramon, Just a thought or 2. I''d still test/spec your gem outside of any Rails app, and I wouldn''t use a sample Rails app in the spec folder. It sounds to me that you are writing a ''acts_as_xxxxx'' type extension to ActiveRecord. You should only need the AR gem installed as a development dep for the gem. You''d need: gem.add_development_dependency "factory_girl", ">= 0" gem.add_development_dependency "rspec", ">= 0" gem.add_dependency ''activerecord'', ">= 2.3" in your Rakefile. Note the use of FactoryGirl. This should help you fake stuff out for testing. No need for the full Rails stack, when all you need is to create some models. In my case, I created a models folder in my gem and put my usual model files in there. Then I require all of them in a Dir[].each block in my soec_helper.rb. With FactoryGirl, you don''t even need to require them first, but in your case you probably need to to get the ''acts_as_xxxx'' functionality. HTH Cheers, Ed Ed Howland http://greenprogrammer.wordpress.com http://twitter.com/ed_howland On Tue, Mar 30, 2010 at 11:22 AM, Ramon Tayag <ramon.tayag at gmail.com> wrote:> Thanks for your reply aslak. Since then I''ve tried another approach - having > a sample Rails application in the spec folder, much like shoulda. Still > haven''t gotten it to work, but I''ll try anyway. > If I have a separate Rails app (which sounds MUCH easier to do - thank you), > won''t it be slow doing TDD because I''d have to publish my gem, update it in > the Rails app, run tests, then back to the gem source code to make changes? > I wasn''t too ambitious with my first gem. It allows subscriptions. The name > "acts_as_subscribable" should be descriptive enough :) > > Ramon Tayag > > On Tue, Mar 30, 2010 at 7:09 PM, aslak hellesoy <aslak.hellesoy at gmail.com> > wrote: >> >> >> On Tue, Mar 30, 2010 at 12:17 PM, Ramon Tayag <ramon.tayag at gmail.com> >> wrote: >>> >>> Hey everyone! >>> >>> A few days ago, I tried my hand at creating a gem. I''ve been putting it >>> off for a long time because of the lack of documentation (or the lack of my >>> Googling skills). When I search how to create gems with RSpec, I get pages >>> that talk about gem templates with the rspec option, as if that would solve >>> it all. >> >> Have you tried jeweler? Try this: >> >> gem install jeweler >> jeweler --help >> jeweler --rspec --summary "My awesome gem, which solves problems" >> --description "The awesome gem" awesome >> cd awesome >> rake -T >> rake spec # write a real spec >> rake version:write >> rake install >> >> See the Jeweler docs for more: >> http://wiki.github.com/technicalpickles/jeweler/ >> >>> If there are no tutorials, maybe I could ask for your help in setting it >>> up for a gem: >>> >>> Backgrounder >>> I''m asking because it''s a bit frustrating doing things by trial and >>> error. A lot of the problems I''ve come across were that the files that >>> rspec-rails loads for me aren''t automatically available in the gem, so I >>> have to manually include it. It became a problem when I had to test an >>> ActiveRecord model, and worse when I had to test a controller. Currently, >>> I''m stuck at the point where "get", "put", "post", "delete" aren''t available >>> in the spec. I asked about it in StackOverflow and got the answer, but it >>> doesn''t explain what I have to do to basically replicate those methods. >>> What did I do? >>> As part of my trial and error process, I started including what >>> rspec-rails >>> includes?http://github.com/dchelimsky/rspec-rails/blob/master/lib/spec/rails.rb, >>> and the REST actions don''t seem to trigger an undefined error anymore, but >>> now there''s another "uninitialized constant Rails". So.. before I go >>> further, maybe I''m approaching this wrong since I''m having such a difficult >>> time. >>> What''s the best way to go about this? Is there a way to maybe include >>> Rails and everything it has, for testing purposes? I know that you''re >>> supposed to make gems framework agnostic but for brevity''s sake (and for my >>> own sanity) I''m willing to make it very plugin-like for now. I''ve been >>> reading and researching for 3 days you see :) >> >> I''m not sure what your gem does, but if it has dependencies on Rails, the >> easiest way to develop it (in my experience) is to have a separate Rails app >> that _uses_ your gem, and that is not part of the gem''s source code. Then >> that Rails app could have specs (and maybe Cucumber features) that rely on >> the correct implementation of your gem. >> >> Maybe you could say a little more about what your gem does? >> >> Aslak >> >>> >>> Thanks, and I apologize for the novel! >>> Ramon Tayag >>> _______________________________________________ >>> 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 >
Thanks Ed I''ll give this a shot too. I was able to get the activerecord part working though. My problem was testing the controller that I wanted to generate for the rails app. Things that I''m used to being in the controller spec (get, put, etc) aren''t, because this RSpec installation is outside of rails. If I could do this without putting a whole app I''d gladly do so as it sounds cleaner. Those methods come from action_controller/test_unit or active_support/test_unit. I tried including both but it didn''t work, and it''s still fuzzy to me. Now, if I waste too much time figuring this out, I may just do what aslak suggested: put those tests in a rails app and write tests for the app that is using this gem. In my mind it sounds slow, as aslak probably has an automatic method to do this, but it definitely sounds easier than learning what needs to be included/required. On Wednesday, March 31, 2010, Ed Howland <ed.howland at gmail.com> wrote:> Hi Ramon, > > Just a thought or 2. > > I''d still test/spec your gem outside of any Rails app, and I wouldn''t > use a sample Rails app in the spec folder. > > It sounds to me that you are writing a ''acts_as_xxxxx'' type extension > to ActiveRecord. You should only need the AR gem installed as a > development dep for the gem. > > You''d need: > ? ?gem.add_development_dependency "factory_girl", ">= 0" > ? ?gem.add_development_dependency "rspec", ">= 0" > ? ?gem.add_dependency ''activerecord'', ">= 2.3" > > in your Rakefile. Note the use of FactoryGirl. This should help you > fake stuff out for testing. No need for the full Rails stack, when all > you need is to create > some models. > > In my case, I created a models folder in my gem and put my usual model > files in there. Then I require all of them in a Dir[].each block in my > soec_helper.rb. > With FactoryGirl, you don''t even need to require them first, but in > your case you probably need to to get the ''acts_as_xxxx'' > functionality. > > HTH > > > Cheers, > Ed > > Ed Howland > http://greenprogrammer.wordpress.com > http://twitter.com/ed_howland-- Ramon Tayag - what you think, we ink http://theinksquad.com