29

I am new to developing Ruby gems, but I thought I'd give it a try.

Recently checking out the latest episode on Railscasts (http://railscasts.com/episodes/245-new-gem-with-bundler) I'm using Bundler to create my gem.

However I'm kind of clueless on how to test my gem.

Sure I could run rake install and then require it from irb, but this seems like a kind of slow workflow for me.

What I'd like to do is create a dummy Rails app and require the gem by referencing to it's source code. Is this possible? I'm sure I've read about it somewhere…

Thanks!

Erik
  • 291
  • 1
  • 3
  • 3
  • ps. Just noticed I could do a "require 'lib/my_gem'" in irb and I don't need to install it. But what if I want to do the same thing but in Rails? ds. – Erik Dec 20 '10 at 19:42

4 Answers4

35

You can include these lines in your Rakefile:

task :console do
  exec "irb -r mygem -I ./lib"
end

This will create a rake task to initialize a new irb session and preload your library. Now, all you have to do is:

$ rake console
Javier Vidal
  • 1,311
  • 11
  • 8
  • 1
    Automated testing is great, but sometimes you need to poke around to figure out how things work. This facilitates that nicely. Replace `irb` with `pry` for even more bang for your buck. – ihaztehcodez Apr 07 '15 at 14:29
24

I can't recommend this guide from Ryan Bigg enough: http://bundler.io/v1.16/guides/creating_gem.html. It walks you through generating a gem using Bundler and setting up automated testing. You can develop your features without ever actually having to run your code manually. It's a workflow I'm using for my own gem development and has worked very well so far.

UPDATE: Rereading your question, it sounds like your gem is a Rails engine. I'd recommending looking at José Valim's EngineX. It's a generator that creates a gem with a dummy Rails app for testing (https://github.com/josevalim/enginex). If you already have a lot of code, http://keithschacht.com/creating-a-rails-3-engine-plugin-gem/ might help you setup a dummy app for testing.

GargantuChet
  • 5,691
  • 1
  • 30
  • 41
Joe Fiorini
  • 641
  • 5
  • 15
13

You can actually do:

cd ~/my_gem_path & bundle console

And this will be a quick test:

My::Gem::VERSION

mcKain
  • 437
  • 5
  • 16
  • 1
    Cool, if you are using `pry` instead of `irb`, there's an extra (on-time) step: `bundle config console pry` – dimid May 03 '17 at 12:31
6

You can reference the gem locally, but if you don't want to run bundle update each time you change the gem, you can just require the files using their full path, or by moving (or symlinking) your gem into /vendor.

But to be honest, it sounds like you need to write some tests! ;) Manually testing each change you make is going to get tiresome and error-prone. If you're new to writing gems, have a look at the source of other popular gems and see how they're tested.

Community
  • 1
  • 1
idlefingers
  • 31,659
  • 5
  • 82
  • 68
  • Haha, yes, after reading your answer and Joe's also, I can see the benefit of testing. Thanks! – Erik Dec 20 '10 at 21:25
  • Manual testing is great, but you also need to be able to manually walk through your code with an aim towards spotting use-cases your tests are missing. Plus tests don't always perfectly reproduce 'live' work, and you have to go through the code at least once to find those. – RonLugge Dec 16 '14 at 17:31