【问题标题】:run minitest programmatically以编程方式运行 minitest
【发布时间】:2012-08-03 11:50:25
【问题描述】:

我有这个代码(在撬):

class Hash
  def invert_by_array
    each.with_object({}) {|(key,value), obj|
      value.each do |element|
        obj[element] = key
      end
    }
  end
end

require 'minitest/spec'

describe "invert_by_array" do
  it "should use the array element as keys" do
    {'foo' => %w(bar baz)}.invert_by_array.must_equal({'bar' => 'foo', 'baz' => 'foo'})
  end
end

如何从编辑运行底部的测试 - 基本上将上面的内容复制/粘贴到 readline?

【问题讨论】:

  • 旁注:Hash[flat_map { |k, vs| vs.product([k]) }].

标签: ruby minitest pry


【解决方案1】:

您可以通过调用MiniTest::Unit.runner.run来调用运行器

class Hash
  def invert_by_array
    each.with_object({}) {|(key,value), obj|
      value.each do |element|
        obj[element] = key
      end
    }
  end
end

require 'minitest/spec'

describe "invert_by_array" do
  it "should use the array element as keys" do
    {'foo' => %w(bar baz)}.invert_by_array.must_equal({'bar' => 'foo', 'baz' => 'foo'})
  end
end

MiniTest::Unit.runner.run

【讨论】:

  • 两次编辑后,您有两个测试。不是我想要的。可以直接运行describe返回的对象吗?
  • 现在,我不再需要“minitest/spec”并以MiniTest::Unit.runner.run 结尾,而是需要“minitest/autorun”并删除最后一行。测试将在 Ruby 退出脚本之前自动运行。
猜你喜欢
  • 2017-07-09
  • 2022-12-20
  • 2011-11-08
  • 1970-01-01
  • 2011-08-16
  • 2019-04-01
  • 2014-08-19
  • 2021-03-11
  • 2021-09-18
相关资源
最近更新 更多