Shoulda Matchers

forked from Shoulda Matchers

https://github.com/thoughtbot/shoulda-matchers

Shoulda Matchers provides RSpec- and Minitest-compatible one-liners to test common Rails functionality that, if written by hand, would be much longer, more complex, and error-prone.
testing 1,079 1


y.fkhar
y.fkhar

Jun 20, 2020

Copy and paste this code into your terminal

DISCLAIMER: You should always review templates before running them. By running the template, you are agreeing to the terms of use.

The contents of this script as show. Any updates will be reflected in the below code and the snippet.

if !defined? RSpec
  say "RSpec was not installed. Adding it now..."
  gem "rspec-rails" , group: [:development, :test]
  run "bundle install"
  generate "rspec:install"
end

gem 'shoulda-matchers', group: [:test]

run 'bundle install'

inject_into_file 'spec/rails_helper.rb', after: '# config.filter_gems_from_backtrace("gem name")' + "\nend" do <<~EOF
  \n
  Shoulda::Matchers.configure do |config|
    config.integrate do |with|
      with.test_framework :rspec
      with.library :rails
    end
  end
  EOF
end

A place where you can thank the author, post problems, give constructive feedback, etc. Be nice!

  • y.fkhar
    What about this version?

    def shoulda_matchers_config(framework = 1)
      f = [nil, "rspec", "minitest"].freeze
    
      <<~EOF
    
        Shoulda::Matchers.configure do |config|
          config.integrate do |with|
            with.test_framework :#{f[framework]}
            with.library :rails
          end
        end
      EOF
    end
    
    if !defined? RSpec
      say "RSpec was not installed. Adding it now..."
      gem "rspec-rails" , group: [:development, :test]
      run "bundle install"
      generate "rspec:install"
    end
    
    gem "shoulda-matchers", group: [:test]
    
    run "bundle install"
    
    answer = ask <<~EOF
      Would you like to use shoulda matchers gem with RSpec or Minitest?
      [1] - RSpec
      [2] - Minitest
    EOF
    
    File.open("#{Rails.root}/spec/rails_helper.rb", "a") do |f|
      f << shoulda_matchers_config(answer.to_i)
      f.close
    end
    
    say("✅ Shoulda-matchers is installed successfully, Happy Hacking")
    by y.fkhar at 6:27PM, 20 Jun 2020
  • Dave Kimura
    Random comment, but I love it that Action Text supports storing emojis 😍
    by Dave Kimura at 12:35PM, 21 Jun 2020
  • Dave Kimura
    I think that the question for RSpec or Minitest is a bit redundant since you are checking if RSpec is defined or not and install it automatically. I would put a prompt within that IF statement that RSpec was not installed, would you like to install it now? If they did choose to install it then you wouldn't need to ask the following question. So the shoulda_matchres_config should default to minitest, but then get overridden to rspec if they chose to install rspec or if they already have it installed.
    by Dave Kimura at 12:39PM, 21 Jun 2020
  • y.fkhar
    Thank you Dave for those remarks and for this project, It's my first year in Ruby, i'll refactor the code to keep it DRY.

    i'll add a case where Rails app is initialized without minitest to ensure the existance of test/test_helper.rb before bundle factory_bot gem. 
    # Skip Test::Unit files          
    rails new app --skip-test-unit
    by y.fkhar at 6:19PM, 21 Jun 2020