RSpec matchers and DSLs for testing Rails 3 generators
MIT License
This project contains RSpec 2 matchers, helpers and various utilities to assist in writing Generator specs. There is additional support for writing specs for Generators in Rails 3.
Rails 3 has a Rails::Generators::TestCase class for use with Test-Unit, to help test generators. This TestCase contains specific custom assertion methods that can be used to assert generator behavior. To create an RSpec 2 equivalent, I wrapped Rails::Generators::TestCase for use with RSpec 2 and created some RSpec 2 matchers that mimic the assertion methods of the Test-Unit TestCase. I have also a bunch of "extra goodies" to the mix.
This RSpec DSL should make it very easy and enjoyable to spec and test your Generators with RSpec 2 :)
Please let me know if you find any issues or have suggestions for improvements.
gem install generator-spec
The gem is a jewel based on jeweler. To install the gem from the code, simply use the jeweler rake task:
rake install
The following demonstrates usage of this library. There are many more options and DSL convenience methods (see wiki, code or specs).
First setup the spec_helper.rb. Here is an example configuration.
I recommend having a separate spec file for each generator (generator specs). You can use the special require_generator statement to ensure that one or more generators are loaded and made available for a given spec.
This will load the generator : generators/canabale_generator.rb
If the generator is in a namespace (subfolder of generators), use a nested approach like this:
This will load the generators: generators/canable/model_generator.rb
and generators/canable/user_generator.rb
You can also load generators from multiple namespaces and mix and match like this. I recommend against this however as it is difficult to read.
You can also require all generators or all within a specific namespace like this:
require_generators :all require_generators :canable => :all
There are a bunch of specialized ruby code matchers in the matchers/content folder which can be used to spec code files in general. Check out the specs in spec/generator_spec/matchers/content for examples on how to use these.
The rails_helpers folder contains a bunch of files which makes it easy to spec rails files and to perform various "Rails mutations". These mutations make it easy to setup the temporary Rails app in a specific pre-condition, which is required for a given spec.
require File.expand_path(File.dirname(FILE) + '/../../../spec_helper')
describe 'controller' do include RSpec::Rails::Controller
before :each do create_controller :account do %q{ def index end } end end
after :each do remove_controller :account end
it "should have an account_controller file that contains an AccountController class with an index method inside" do Rails.application.should have_controller :account do |controller_file| controller_file.should have_controller_class :account do |klass| klass.should have_method :index end end end end
Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.