http://github.com/kristianmandrup/rspec_for_generators
When creating Rails generators I noticed that the only option I could
find for testing was to use a special Rails TestCase class created
specifically for use with Test Unit.
So I thought I could wrap it to be used with RSpec 2 instead. I now
have a working first release with room for lots of improvements! But
at least it seems to work fine for now, as demonstrated by a small
RSpec 2 test suite that comes with it.
Note: This library is NOT in any way an official RSpec 2 release of
any sort. I only use the RSpec name to indicate that it is an add-on
component for RSpec 2. That it can be used with RSpec 2.
Not sure what other convention there would be to indicate this?
Maybe I should rename it to something like the following to make it
more clear?
rspec2-addon-for-generators
Hope you find it useful as a starting point. Free feel to patch it,
refactor etc.
Enjoy!
Kristian
On Aug 3, 2010, at 3:06 PM, Kristian M. wrote:
Note: This library is NOT in any way an official RSpec 2 release of
any sort. I only use the RSpec name to indicate that it is an add-on
component for RSpec 2. That it can be used with RSpec 2.
Not sure what other convention there would be to indicate this?
Maybe I should rename it to something like the following to make it
more clear?
rspec2-addon-for-generators
Ben M. called his email-spec. So maybe generator-spec?
Alright, I will rename it when I get to it. Just improved it in a
major fashion! Now as simple as this to set it up
spec/spec_helper.rb
require ‘rspec’
require ‘rspec_for_generators’
Rails.application.configure do
use default rails tmp dir
config.root_dir = TmpRails.root_dir(FILE)
end
configure it!
RSpec::Generator.configure!
And to use it in a spec:
require File.expand_path(File.dirname(FILE) + ‘/…/spec_helper’)
require_generator :demo
describe ‘model_generator’ do
include RSpec::Rails::Orm::ActiveRecord
before :each do
RSpec::Generator.setup_generator ‘model_generator’ do
tests DemoGenerator
end
remove_model ‘account’
end
after :each do
remove_model ‘account’
end
it “should decorate an existing Account model file with include
Demo” 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 do |klass|
klass.should include_module ‘Demo’
end
end
end
end
end
Even simpler config now
require ‘rspec’
require ‘rspec_for_generators’
configure it like this to use default settings
RSpec::Generator.configure_root_dir(FILE)