Module: Install::RSpec

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

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



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

def self.included(thor)
  thor.class_eval do
    
    desc 'add_rspec_gem', 'Add RSpec gem to Gemfile and run bundler'
    def add_rspec_gem
      insert_into_file 'Gemfile', after: "gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]\n" do <<-CODE
gem 'rspec-rails', group: :test
CODE
      end

      run 'bundle install'
    end
    
    desc 'generate_rspec', 'Run RSpec generator'
    def generate_rspec
      run 'rails g rspec:install'
    end
    
    desc 'setup_rspec_defaults', 'add default entris to spec/rails_helper.rb'
    def setup_simplecov_capybara
      inject_into_file "spec/rails_helper.rb", after: "require 'rspec/rails'\n" do <<-CODE
require 'simplecov'
SimpleCov.start

Capybara.app_host = "http://localhost:3000"
Capybara.server_host = "localhost"
Capybara.server_port = "3000"

#use Chromedriver
unless ENV['NOCHROME']
  Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
  end
end
CODE
      end
    end
    
    desc 'enable_support', 'Enable support files'
    def enable_support
      gsub_file 'spec/rails_helper.rb', "# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }", "Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }"
    end
    
    desc 'disable_transctional_fixtures', 'Turn off transactional fixtures option'
    def disable_transctional_fixtures
      gsub_file "spec/rails_helper.rb", "config.use_transactional_fixtures = true", "config.use_transactional_fixtures = false"
    end
    
    
    desc 'include_modules', 'Include useful modules used in feature specs'
    def include_modules
      inject_into_file 'spec/rails_helper.rb', after: "RSpec.configure do |config|\n" do <<-CODE
  # Can use methods like dom_id in features
  config.include ActionView::RecordIdentifier, type: :feature
  # Can use methods likke strip_tags in features
  config.include ActionView::Helpers::SanitizeHelper, type: :feature
  # Can use methods like truncate
  config.include ActionView::Helpers::TextHelper, type: :feature
  config.include(JavascriptHelper, type: :feature)
  config.include MailerHelper
CODE
      end
    end

    desc 'install_rails_helper', 'Add code to rspec/rails_helper so rspec runs the way I like'
    def setup_rails_helper
      setup_simplecov_capybara
      enable_support
      disable_transctional_fixtures
      include_modules
    end

    desc 'setup_support_files', 'Generate RSpecsupport files'
    def setup_support_files
      Dir["#{__dir__}/../templates/spec/**/*"].each do |file|
        if file.include?('/support/') && !['devise'].include?(File.basename(file, '.rb'))
          copy_file file, "#{file.gsub(__dir__+'/../templates/', '')}" unless File.directory? file
        end
      end
    end
    
    desc 'setup_rspec', 'Generate rspec structure & rspec configuration that I commonly use'
    def setup_rspec
      add_rspec_gem
      generate_rspec
      setup_rails_helper
      setup_support_files
    end
    
  end
end