在所有MiniTest测试中包含模块,例如RSpec中的 [英] Include module in all MiniTest tests like in RSpec

查看:62
本文介绍了在所有MiniTest测试中包含模块,例如RSpec中的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在RSpec中,我可以在/spec/support/...

In RSpec I could create helper modules in /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

如何在不进行重复测试的情况下将模块包含在所有MiniTest测试中?

How can I include a module into all MiniTest tests without repeating myself in every test?

推荐答案

minitest不能提供以与RSpec相同的方式将includeextend模块插入每个测试类的方法

minitest does not provide a way to include or extend a module into every test class in the same way RSpec does.

您最好的选择是重新打开测试用例类(不同,具体取决于您所使用的最小测试版本),并include在此处使用任何模块.您可能想在test_helper或专用文件中执行此操作,让其他所有人知道您是猴子补丁最小测试.以下是一些示例:

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. 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. Here are some examples:

对于最小测试〜> 4(Ruby标准库提供了什么)

For minitest ~> 4 (what you get with the Ruby Standard Library)

module MiniTest
  class Unit
    class TestCase
      include MyHelpers
    end
  end
end

对于最小的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!

这篇关于在所有MiniTest测试中包含模块,例如RSpec中的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆