1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
@example_app
Feature: generator spec
Generator specs live in spec/generators. In order to access
the generator's methods you can call them on the "generator" object.
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
class_option :someone, :type => :string
def create_awesomeness
template 'awesome.html', File.join('public', name, "#{"super_" if options[:super]}awesome.html")
end
def create_lameness
template 'lame.html.erb', File.join('public', name, "#{"super_" if options[:super]}lame.html")
end
end
"""
And a file named "lib/generators/awesome/templates/awesome.html" with:
"""
This is an awesome file
"""
And a file named "lib/generators/awesome/templates/lame.html.erb" with:
"""
<%= options[:someone] %> is lame
"""
Scenario: A spec that runs the entire generator
Given a file named "spec/generators/awesome_generator_spec.rb" with:
"""
require "rails_helper"
require 'generators/awesome/awesome_generator'
describe AwesomeGenerator do
describe 'invoke' do
before { run_generator %w(my_dir --someone Alex) }
describe 'public/my_dir/awesome.html' do
subject { file('public/my_dir/awesome.html') }
it { expect(subject).to exist }
it { expect(subject).to contain 'This is an awesome file' }
it { expect(subject).to_not contain 'This text is not in the file' }
end
describe 'public/my_dir/lame.html' do
subject { file('public/my_dir/lame.html') }
it { expect(subject).to exist }
it { expect(subject).to contain 'Alex is lame' }
it { expect(subject).to_not contain 'This text is not in the file' }
end
end
describe 'revoke' do
subject { file('public/my_dir/awesome.html') }
it 'can run a reverse migration' do
FileUtils.mkdir_p(File.dirname(subject))
# File.write is not in 1.8.7 use File.open
File.open(subject, 'w') do |f|
f.write "test file"
end
expect(subject).to exist
run_generator %w(my_dir --someone Alex), :behavior => :revoke
expect(subject).not_to exist
end
end
end
"""
When I run `rake spec`
Then the output should contain "7 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 "rails_helper"
require 'generators/awesome/awesome_generator'
describe AwesomeGenerator do
arguments %w(another_dir)
before { invoke_task :create_awesomeness }
describe 'public/another_dir/awesome.html' do
subject { file('public/another_dir/awesome.html') }
it { expect(subject).to exist }
it { expect(subject).to contain 'This is an awesome file' }
it { expect(subject).to_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 "4 examples, 0 failures"
Scenario: A spec with some failures shows nice error messages
Given a file named "spec/generators/awesome_generator_spec.rb" with:
"""
require "rails_helper"
require 'generators/awesome/awesome_generator'
describe AwesomeGenerator do
before { run_generator %w(my_dir) }
describe 'public/my_dir/awesome.html' do
subject { file('public/my_dir/awesome.html') }
it { expect(subject).to_not contain 'This is an awesome file' }
it { expect(subject).to contain 'This text is not in the file' }
it { expect(subject).to_not exist }
end
describe 'public/my_dir/non_existent.html' do
subject { file('public/my_dir/non_existent.html') }
it { expect(subject).to exist }
end
describe 'db/migrate/non_existent_migration.rb' do
subject { migration_file('db/migrate/non_existent_migration.rb') }
it { expect(subject).to exist }
end
end
"""
When I run `rake spec`
Then the output should contain "5 examples, 5 failures"
And the output should contain:
"""
/public/my_dir/awesome.html to not contain "This is an awesome file" but it did
"""
And the output should contain:
"""
/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:
"""
/public/my_dir/awesome.html" not to exist
"""
And the output should contain:
"""
/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 "rails_helper"
require 'generators/awesome/awesome_generator'
describe AwesomeGenerator do
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 { expect(subject).to exist }
end
describe 'public/my_dir/super_lame.html' do
subject { file('public/my_dir/super_lame.html') }
it { expect(subject).to 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 "rails_helper"
require 'rails/generators/active_record/migration/migration_generator'
describe ActiveRecord::Generators::MigrationGenerator do
before { run_generator %w(create_posts) }
subject { migration_file('db/migrate/create_posts.rb') }
it { expect(subject).to exist }
it { expect(subject).to be_a_migration }
it { expect(subject).to contain 'class CreatePosts < ActiveRecord::Migration' }
end
"""
When I run `rake spec`
Then the output should contain "3 examples, 0 failures"
Scenario: Can tell the generator where to put its files
Given a file named "spec/generators/awesome_generator_spec.rb" with:
"""
require "rails_helper"
require 'generators/awesome/awesome_generator'
describe AwesomeGenerator do
destination Rails.root + 'tmp/generated_files'
before { run_generator %w(my_dir --super) }
describe 'public/my_dir/super_awesome.html' do
subject { file('public/my_dir/super_awesome.html') }
it { expect(subject).to eq "#{Rails.root}/tmp/generated_files/public/my_dir/super_awesome.html" }
it { expect(subject).to exist }
end
end
"""
When I run `rake spec`
Then the output should contain "2 examples, 0 failures"
|