Now with a full RSpec 2 test suite to demonstrate most of its
features. Wiki to be updated with latest changes ASAP.
http://github.com/kristianmandrup/rspec_for_generators
This gem makes it very easy to spec that your generator generates
files as expected and also lets you easily verify that the content of
those files match what you expect
- name of the class
- subclass of class
- class includes specific modules
- contains certain instance or class methods
…
In general it has a lot of nice matchers to match on generated Ruby
source code (using regexp)
It also has a nice DSL to spec generated migration files in detail
(see below)
spec generated model file
it “should decorate an existing Account model file with include
Canable:Ables” do
with_generator do |g|
name = ‘account’
create_model name
g.run_generator [name]
g.should generate_model name do |content|
content.should have_class name do |klass|
klass.should include_module ‘Canable::Ables’
end
end
end
end
spec generated migration file
g.should generate_migration name do |content|
content.should have_migration name do |klass|
klass.should have_up do |up|
up.should have_create_table :users do |user_tbl|
user_tbl.should have_columns :name => :string, :age
=> :string
end
end
klass.should have_down do |up|
up.should have_drop_table :users
end
end
end
spec generated Active Record observer
g.run_generator [name]
g.should generate_observer name do |content|
content.should have_observer_class name do |klass|
klass.should have_method :observe_me
end
end
and many more…