繁体   English   中英

默认情况下Minitest-Rails Spec-Style测试

[英]Minitest-Rails Spec-Style Tests by Default

问题:使用Rails 5和Minitest-Rails有没有办法将新的Rails应用程序默认为Spec-style测试?

我教TDD,每次我们制作一个新的应用程序时都要让学生转换它很烦人。

您可以使用以下配置创建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存储库,并在此基础上构建。

参考文献:

看起来你已经完成了回答问题的艰苦工作。 虽然如果您正在教授一组具有固定意义的测试宝石,以及修改过的test_helper.rb和修改过的application.rb,您可能希望考虑编写自己的宝石,学生可以将其添加到他们的Gemfile中。 gem可以将您想要的测试宝石作为依赖项,然后他们可以安装他们需要的所有其他内容,例如:

bin/rails generate <gem_name>:install

这是我写的一个宝石,你可以分叉或修改或只是用作灵感。

https://github.com/schadenfred/testable

我实际上偷了你上面的宝石的配置代码,你可以看到它存在于生成器中的生成器里面:

LIB /发电机/安装/ install_generator.rb

看起来在config/application.rb你只需要添加:

config.generators do |g| g.test_framework :minitest, spec: true end

但是,没有一种自动方法可以将Minitest-Rails默认为spec样式测试。

我可以去rspec,但我宁愿和Minitest呆在一起,因为我们从一开始就教我们的学生Minitest。

好的,所以@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 for DB,Minitest-rails使用spec-style测试并包括minitest-reporter。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM