简体   繁体   English

在RSpec中的所有MiniTest测试中包含模块

[英]Include module in all MiniTest tests like in RSpec

In RSpec I could create helper modules in /spec/support/... 在RSpec中,我可以在/spec/support/...创建辅助模块/spec/support/...

module MyHelpers
  def help1
    puts "hi"
  end
end

and include it in every spec like this: 并将其包含在每个规范中:

RSpec.configure do |config|
  config.include(MyHelpers)
end

and use it in my tests like this: 并在我的测试中使用它,如下所示:

describe User do
  it "does something" do
    help1
  end
end

How can I include a module into all MiniTest tests without repeating myself in every test? 如何在所有MiniTest测试中包含一个模块,而不必在每次测试中重复自己?

From the Minitest README: 从Minitest README:

=== How to share code across test classes?

Use a module. That's exactly what they're for:

module UsefulStuff
  def useful_method
    # ...
  end
end

describe Blah do
  include UsefulStuff

  def test_whatever
    # useful_method available here
  end
end

Just define the module in a file and use require to pull it in. For example, if 'UsefulStuff' is defined in test/support/useful_stuff.rb, you might have require 'support/useful_stuff' in either your individual test file. 只需在文件中定义模块并使用require将其拉入。例如,如果在test / support / useful_stuff.rb中定义了'UsefulStuff',则可能在您的单个测试文件中需要'support / useful_stuff'。

UPDATE: 更新:

To clarify, in your existing test/test_helper.rb file or in a new test/test_helper.rb file you create, include the following: 为了澄清,在您现有的test / test_helper.rb文件或您创建的新test / test_helper.rb文件中,包含以下内容:

Dir[Rails.root.join("test/support/**/*.rb")].each { |f| require f }

which will require all files in the test/support subdirectory. 这将需要test / support子目录中的所有文件。

Then, in each of your individual test files just add 然后,在每个单独的测试文件中添加

require 'test_helper'

This is exactly analogous to RSpec, where you have a require 'spec_helper' line at the top of each spec file. 这与RSpec完全类似,在每个spec文件的顶部都有一个require'pect_helper'行。

minitest does not provide a way to include or extend a module into every test class in the same way RSpec does. minitest没有像RSpec那样提供 extend模块includeextend到每个测试类的方法。

Your best bet is going to be to re-open the test case class (differs, depending on the minitest version you're using) and include whatever modules you want there. 您最好的选择是重新打开测试用例类(根据您使用的最小版本而有所不同)并include您想要的任何模块。 You probably want to do this in either your test_helper or in a dedicated file that lets everyone else know you're monkey-patching minitest. 您可能希望在test_helper或专用文件中执行此操作,该文件可让其他人知道您是最小的猴子修补程序。 Here are some examples: 这里有些例子:

For minitest ~> 4 (what you get with the Ruby Standard Library) 对于minitest~> 4(你使用Ruby标准库得到的)

module MiniTest
  class Unit
    class TestCase
      include MyHelpers
    end
  end
end

For minitest 5+ 最小的5+

module Minitest
  class Test
    include MyHelperz
  end
end

You can then use the included methods in your test: 然后,您可以在测试中使用包含的方法:

class MyTest < Minitest::Test # or MiniTest::Unit::TestCase
  def test_something
    help1
    # ...snip...
  end
end

Hope this answers your question! 希望这能回答你的问题!

One thing I will do is create my own Test class inheriting from Minitest::Test . 我要做的一件事是创建我自己的继承自Minitest::TestTest类。 This allows me to do any sort of configuration on my base test class and keeping it isolated to my own project 1 . 这允许我在我的基础测试类上进行任何类型的配置,并使其与我自己的项目隔离1

# test_helper.rb
include 'helpers/my_useful_module'
module MyGem
  class Test < Minitest::Test
    include MyUsefulModule
  end
end

# my_test.rb
include 'test_helper'
module MyGem
  MyTest < Test
  end
end

1 This is most likely unneeded, but I like keeping all my gem code isolated. 1这很可能是不必要的,但我喜欢保持所有宝石代码的隔离。

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

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