features/generator_spec.feature in ammeter-0.1.3 vs features/generator_spec.feature in ammeter-0.2.0
- old
+ new
@@ -6,17 +6,18 @@
Background: A simple generator
Given a file named "lib/generators/awesome/awesome_generator.rb" with:
"""
class AwesomeGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
+ class_option :super, :type => :boolean, :default => false
def create_awesomeness
- template 'awesome.html', File.join('public', name, 'awesome.html')
+ template 'awesome.html', File.join('public', name, "#{"super_" if options[:super]}awesome.html")
end
def create_lameness
- template 'lame.html', File.join('public', name, 'lame.html')
+ template 'lame.html', File.join('public', name, "#{"super_" if options[:super]}lame.html")
end
end
"""
And a file named "lib/generators/awesome/templates/awesome.html" with:
"""
@@ -34,43 +35,144 @@
require 'generators/awesome/awesome_generator'
describe AwesomeGenerator do
destination File.expand_path("../../tmp", __FILE__)
- before do
- run_generator %w(my_dir)
+ before { run_generator %w(my_dir) }
+ describe 'public/my_dir/awesome.html' do
+ subject { file('public/my_dir/awesome.html') }
+ it { should exist }
+ it { should contain 'This is an awesome file' }
+ it { should_not contain 'This text is not in the file' }
end
- it 'should copy the awesome file into public' do
- file('public/my_dir/awesome.html').should exist
+ describe 'public/my_dir/lame.html' do
+ subject { file('public/my_dir/lame.html') }
+ it { should exist }
+ it { should contain 'This is a lame file' }
+ it { should_not contain 'This text is not in the file' }
end
- it 'should copy the lame file into public' do
- file('public/my_dir/lame.html').should exist
+ end
+ """
+ When I run `rake spec`
+ Then the output should contain "6 examples, 0 failures"
+
+ Scenario: A spec that runs one task in the generator
+ Given a file named "spec/generators/another_awesome_generator_spec.rb" with:
+ """
+ require "spec_helper"
+ require 'generators/awesome/awesome_generator'
+
+ describe AwesomeGenerator do
+ destination File.expand_path("../../tmp", __FILE__)
+ arguments %w(another_dir)
+
+ before { invoke_task :create_awesomeness }
+ describe 'public/another_dir/awesome.html' do
+ subject { file('public/another_dir/awesome.html') }
+ it { should exist }
+ it { should contain 'This is an awesome file' }
+ it { should_not contain 'This text is not in the file' }
end
+ describe 'public/another_dir/lame.html' do
+ subject { file('public/another_dir/lame.html') }
+ it { should_not exist }
+ end
end
"""
When I run `rake spec`
- Then the output should contain "2 examples, 0 failures"
+ Then the output should contain "4 examples, 0 failures"
- Scenario: A spec that runs one task in the generator
- Given a file named "spec/generators/another_awesome_generator_spec.rb" with:
- """
- require "spec_helper"
- require 'generators/awesome/awesome_generator'
+ Scenario: A spec with some failures shows good error messages
+ Given a file named "spec/generators/awesome_generator_spec.rb" with:
+ """
+ require "spec_helper"
+ require 'generators/awesome/awesome_generator'
- describe AwesomeGenerator do
- destination File.expand_path("../../tmp", __FILE__)
- arguments %w(another_dir)
+ describe AwesomeGenerator do
+ destination File.expand_path("../../tmp", __FILE__)
- before do
- invoke_task :create_awesomeness
- end
- it 'should copy the awesome file into public' do
- file('public/another_dir/awesome.html').should exist
- file('public/another_dir/awesome.html').should contain 'This is an awesome file'
- end
- it 'should not have copied the lame file into public' do
- file('public/another_dir/lame.html').should_not exist
- end
+ before { run_generator %w(my_dir) }
+ describe 'public/my_dir/awesome.html' do
+ subject { file('public/my_dir/awesome.html') }
+ it { should_not contain 'This is an awesome file' }
+ it { should contain 'This text is not in the file' }
+ it { should_not exist }
end
- """
- When I run `rake spec`
- Then the output should contain "2 examples, 0 failures"
\ No newline at end of file
+ describe 'public/my_dir/non_existent.html' do
+ subject { file('public/my_dir/non_existent.html') }
+ it { should exist }
+ end
+ describe 'db/migrate/non_existent_migration.rb' do
+ subject { migration_file('db/migrate/non_existent_migration.rb') }
+ it { should exist }
+ end
+ end
+ """
+ When I run `rake spec`
+ Then the output should contain "5 examples, 5 failures"
+ And the output should contain:
+ """
+ /tmp/public/my_dir/awesome.html to not contain "This is an awesome file" but it did
+ """
+ And the output should contain:
+ """
+ /tmp/public/my_dir/awesome.html to contain "This text is not in the file" but it contained "This is an awesome file"
+ """
+ And the output should contain:
+ """
+ /tmp/public/my_dir/awesome.html" not to exist
+ """
+ And the output should contain:
+ """
+ /tmp/public/my_dir/non_existent.html" to exist
+ """
+ And the output should contain:
+ """
+ db/migrate/TIMESTAMP_non_existent_migration.rb" to exist
+ """
+
+ Scenario: Can specify arguments separately from running the generator
+ Given a file named "spec/generators/awesome_generator_spec.rb" with:
+ """
+ require "spec_helper"
+ require 'generators/awesome/awesome_generator'
+
+ describe AwesomeGenerator do
+ destination File.expand_path("../../tmp", __FILE__)
+ arguments %w(my_dir --super)
+
+ before { generator.invoke_all }
+ describe 'public/my_dir/super_awesome.html' do
+ subject { file('public/my_dir/super_awesome.html') }
+ it { should exist }
+ end
+ describe 'public/my_dir/super_lame.html' do
+ subject { file('public/my_dir/super_lame.html') }
+ it { should exist }
+ end
+ end
+ """
+ When I run `rake spec`
+ Then the output should contain "2 examples, 0 failures"
+
+ Scenario: A generator that creates a migration
+ Given a file named "spec/generators/a_migration_spec.rb" with:
+ """
+ require "spec_helper"
+ require 'rails/generators/active_record/migration/migration_generator'
+
+ describe ActiveRecord::Generators::MigrationGenerator do
+ destination File.expand_path("../../tmp", __FILE__)
+
+ before do
+ prepare_destination
+ run_generator %w(create_posts)
+ end
+ subject { migration_file('db/migrate/create_posts.rb') }
+ it { should exist }
+ it { should contain 'class CreatePosts < ActiveRecord::Migration' }
+ end
+ """
+ When I run `rake spec`
+ Then the output should contain "2 examples, 0 failures"
+
+