Module: RSpec::Generator::Actions

Defined in:
lib/myrails/modules/rspec_generator_actions.rb

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



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
# File 'lib/myrails/modules/rspec_generator_actions.rb', line 6

def self.included(thor)
  
  thor.class_eval do
    
    desc 'shared_example', 'Generates an RSpec shared example template in the support directory'
    def shared_example
      @description = ask "What is the NAME of shared example?, Ex: requires authorization. Default: ", :yellow, default: 'shared example'
      @context = ask "What is the CONTEXT of the shared example?, Ex: authorized user. Default: ", :yellow, default: 'shared example'
      
      template 'spec/shared_example.rb', "spec/support/shared_examples/#{@param.downcase.gsub("\s", '_')}_shared_examples.rb"
    end

    desc 'request', 'Generates an RSpec request spec'
    def request
      template 'spec/request.rb', "spec/requests/#{@param.downcase.gsub("\s", "_")}_spec.rb"
      copy_file 'spec/request_shared_example.rb', 'spec/support/shared_examples/request_shared_examples.rb'
    end

    desc 'feature', 'Generates an RSpec feature spec'
    def feature
      @description = ask "What is the description of feature?, Ex: user management. Default: ", :yellow, default: 'feature'
      @scenario = ask "What is the CONTEXT of the shared example?, Ex: as a user ... Default: ", :yellow, default: 'scneario'
      
      template 'spec/feature.rb', "spec/features/#{@param.downcase.gsub("\s", '_')}_spec.rb"
    end

    desc 'helper', 'Generates an RSpec helper in support/helpers for extracting reusable code'
    long_desc <<-LONGDESC
    `myrails helper` will generate an RSpec helper module to use with rspec.

    You can optionally specify a type parameter which will only include the module for the given type of spec.

    > $ myrails helper --name article --type :feature
    LONGDESC

    def helper
      @type = ask 'What is the type? Ex: :feature, :controller, :request, :model. Deafult: ', :yellow, default: :feature
      template 'spec/helper.rb', "spec/support/helpers/#{@param.downcase.gsub("\s", '_')}.rb"
      insert_into_file 'spec/rails_helper.rb', after: "RSpec.configure do |config|\n" do <<-CODE
  config.include #{@param.camelize.gsub("\s", '')}Helper#{", type: #{':' unless @type.include?(':')}#{@type}" if @type}
CODE
      end
    end
    
  end
  
end