問題:使用Rails 5 & Minitest-Rails是否有一種方法來默認新的Rails應用程序默認爲Spec樣式測試?Minitest-Rails默認規格樣式測試
我教TDD,每次我們製作一個新應用程序時都要讓學生轉換,這很煩人。
問題:使用Rails 5 & Minitest-Rails是否有一種方法來默認新的Rails應用程序默認爲Spec樣式測試?Minitest-Rails默認規格樣式測試
我教TDD,每次我們製作一個新應用程序時都要讓學生轉換,這很煩人。
好吧@sonna有90%的我正在尋找。
我結束了幫助創建.railsrc
文件,
-d postgresql
-m ~/.template.rb
並與模板:
# .template.rb
# Gems we've talked about in class, but which have absolutely nothing to do
# with setting up spec-style testing.
# Included here for convenience.
gem_group :development do
# Improve the error message you get in the browser
gem 'better_errors'
# Use pry for rails console
gem 'pry-rails'
end
# Add some extra minitest support
gem_group :test do
gem 'minitest-rails'
gem 'minitest-reporters'
end
# Add some code to some files to support spec-style testing
# For these injections, indentation matters!
inject_into_file 'config/application.rb', after: "class Application < Rails::Application\n" do
<<-'RUBY'
# Force new test files to be generated in the minitest-spec style
config.generators do |g|
g.test_framework :minitest, spec: true
end
RUBY
end
# Things to do after all the gems have been installed
after_bundle do
# Run rails generate minitest:install
generate "minitest:install", "--force"
# Add minitest reporters support. This must be run after
# rails generate minitest:install, because that command
# changes test/test_helper.rb
inject_into_file 'test/test_helper.rb', after: 'require "minitest/rails"' do
<<-'RUBY'
require "minitest/reporters" # for Colorized output
# For colorful output!
Minitest::Reporters.use!(
Minitest::Reporters::SpecReporter.new,
ENV,
Minitest.backtrace_filter
)
RUBY
end
end
這是我的項目設置與Postgres的爲DB,並使用規格 - MINITEST護欄風格測試,幷包括微型記者。
您可以創建一個template.rb
文件具有以下配置:
gem_group :development, :test do
gem "rspec-rails"
end
after_bundle do
`rails g rspec:install`
end
,然後建立一個新的Rails項目使用以下命令
rails new my_app -T -m /path/to/template.rb
這將建立一個新的Rails應用程序,添加的Rails的RSpec gem到它的Gemfile並執行RSpec的安裝步驟。
否則,您可以提供預構建的Rails Git存儲庫並在其上構建。
參考文獻:
謝謝,但是我不想使用rspec,而是使用Minitest。 但是,模板可能是要走的路。 –
它看起來像在config/application.rb
你只需要添加:
config.generators do |g| g.test_framework :minitest, spec: true end
但是沒有一種自動的方式可以讓Minitest-Rails默認使用規格樣式測試。
我可以去rspec,但寧願留在Minitest那一刻,因爲我們從一開始就教我們的學生Minitest。
您可以執行'rails new my_app -T',它將跳過測試,然後將rspec添加到gemfile並運行rspec init。這通常是我推薦的 – Anthony
@anthony是的,但是我確實想用Minitest代替rspec進行測試。 –