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
|
Feature: Gems can contain generators
Within a Gem Rails may often not be loaded (but railties is)
We should still be able to write a spec for these generators
@railties_gem
Scenario: A generator with "railties" dependency
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, "#{"super_" if options[:super]}awesome.html")
end
end
"""
And a file named "lib/generators/awesome/templates/awesome.html" with:
"""
This is an awesome file
"""
And a file named "spec/generators/awesome_generator_spec.rb" with:
"""
require "spec_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 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
end
"""
When I run `rake spec`
Then the output should contain "3 examples, 0 failures"
@rails_gem
Scenario: A generator that uses "hook_for"
Given a file named "lib/generators/resourceful/resourceful_generator.rb" with:
"""
class ResourcefulGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)
class_option :super, :type => :boolean, :default => false
hook_for :orm, :in => :rails, :as => :model, :required => true
def create_resourceful_controller
template 'controller.rb', File.join('app/controllers', "#{plural_file_name}_controller.rb")
end
end
"""
And a file named "lib/generators/resourceful/templates/controller.rb" with:
"""
class <%= class_name.pluralize %>Controller < ResourcefulController
end
"""
And a file named "spec/generators/resourceful_generator_spec.rb" with:
"""
require "spec_helper"
require 'generators/resourceful/resourceful_generator'
describe ResourcefulGenerator do
before { run_generator %w(post) }
describe 'app/controller/posts_controller.rb' do
subject { file('app/controllers/posts_controller.rb') }
it { expect(subject).to exist }
it { expect(subject).to contain 'class PostsController < ResourcefulController' }
end
describe 'app/models/post.rb' do
subject { file('app/models/post.rb') }
it { expect(subject).to exist }
it { expect(subject).to contain 'class Post < ' }
end
end
"""
When I run `rake spec`
Then the output should contain "4 examples, 0 failures"
|