File: hooking_into_other_generators.feature

package info (click to toggle)
ruby-ammeter 1.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 312 kB
  • sloc: ruby: 703; makefile: 4
file content (51 lines) | stat: -rw-r--r-- 1,832 bytes parent folder | download | duplicates (2)
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
@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 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
      """

  Scenario: A spec that runs the entire generator
    Given a file named "spec/generators/resourceful_generator_spec.rb" with:
      """
      require "rails_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"