# RSpec 2 library for specifying and testing generators
RSpec 2 matchers, helpers and various utilities to assist in writing generator specs especially Rails 3 generators.
This project was extracted from my experiments with creating generators for jnunemaker's *Canable*, see [My fork of Canable](http://github.com/kristianmandrup/canable).
I attempted to test the generators I built but I noticed, that the only option I could find, was to use a special Rails TestCase class created for Test-Unit.
So I thought I could wrap it to be used with RSpec 2; my BDD testing framework of choice!
## Install
gem install rspec_for_generators
To install using edge
rake install
## Usage
The following demonstrates an example usage. There are many more options and cool DSL convenience methods. Check out the code!
# spec/spec_helper.rb require 'rspec' # This is required for rails_spec_helper.rb to work. Not sure how to better set this configuration value. # It's the relative root for which the Rails mock app is created. So if it's set to point to the spec folder, # then rails will be put under ../tmp, relative to /spec, so that /tmp will be a folder in the current project root. # Note: In the near future I will provide a better way to achieve this by hooking into some step in the Rails 3 loading behavior module Rails def config_root_dir File.dirname(__FILE) end end # load this rspec toolbox to help spec generators require 'rspec_for_generators'# spec/generators/canable.rb require_generators :canable => ['model', 'user']# spec/generators/model_generator_spec.rb require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') # list of generators to spec are loaded require 'generators/canable' describe 'model_generator' do # include Rails model helpers for AciveRecord # also available are: MongoMapper, Mongoid and DataMapper # mongo_mapper, :data_mapper, :mongoid model_helper_for :active_record before :each do # define generator to test RSpec::Generator.setup_generator 'model_generator' do tests Canable::Generators::ModelGenerator end # ensure clean state before run remove_model 'account' end after :each do # ensure clean state after run remove_model 'account' end it "should not work without an existing Account model file" do RSpec::Generator.with_generator do |g| name = 'account' g.run_generator %w{account} g.should_not generate_file name, :model end end it "should decorate an existing Account model file with 'include Canable:Ables'" do RSpec::Generator.with_generator do |g| name = 'account' create_model name g.run_generator %w{account} g.should generate_file name, :model do |content| content.should have_class name.camelize do |klass| klass.should include_module 'Canable::Ables' end end end end end## Note on Patches/Pull Requests * Fork the project. * Make your feature addition or bug fix. * Add tests for it. This is important so I don't break it in a future version unintentionally. * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull) * Send me a pull request. Bonus points for topic branches. ## Copyright Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.