2017-03-08 69 views
1

问题:使用Rails 5 & Minitest-Rails是否有一种方法来默认新的Rails应用程序默认为Spec样式测试?Minitest-Rails默认规格样式测试

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

+1

您可以执行'rails new my_app -T',它将跳过测试,然后将rspec添加到gemfile并运行rspec init。这通常是我推荐的 – Anthony

+0

@anthony是的,但是我确实想用Minitest代替rspec进行测试。 –

回答

0

好吧@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护栏风格测试,并包括微型记者。

0

您可以创建一个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存储库并在其上构建。

参考文献:

+0

谢谢,但是我不想使用rspec,而是使用Minitest。 但是,模板可能是要走的路。 –

0

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

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

但是没有一种自动的方式可以让Minitest-Rails默认使用规格样式测试。

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