[rspec-users] Gem tutorials / setting up rspec in a gem

Ed Howland ed.howland at gmail.com
Tue Mar 30 16:53:01 EDT 2010

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
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'



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

More information about the rspec-users mailing list