【问题标题】:Hello world minitest cli exampleHello world minitest cli 示例
【发布时间】:2016-11-08 22:41:25
【问题描述】:

给定以下目录结构:

➜  ~/D/w/t/foo  tree .
.
├── app.rb
├── foo.rb
└── test
    └── test_foo.rb

1 directory, 3 files

如果我运行app.rb,我会得到经典的“Hello world!”回应。

➜  ~/D/w/t/foo  ruby app.rb
"Hello world!"

看看app.rbfoo.rb内部:

➜  ~/D/w/t/foo  cat app.rb
require_relative 'foo.rb'

Foo.new.bar

➜  ~/D/w/t/foo  cat foo.rb
class Foo
  def bar
    p "Hello world!"
  end
end

我将如何从命令行使用 minitest 测试我的 Foo 类?

我知道它与ruby -Ilib 命令有关,但我不确定确切的开关和标志是什么,我也不确定在test_foo.rb 里面放什么。我也不确定我的目录结构是否设置正确。也许foo.rb 需要进入lib 目录?

希望你指导:)

【问题讨论】:

    标签: ruby bash command-line-interface minitest


    【解决方案1】:

    您可以像运行 ruby​​ 文件一样运行 minitest 测试。只需安装 minitest,编写测试,然后像运行 ruby​​ 脚本一样运行:

    安装小测试:

    ➜  ~/D/w/t/hello-minitest  gem install minitest
    Successfully installed minitest-5.9.1
    Parsing documentation for minitest-5.9.1
    Done installing documentation for minitest after 0 seconds
    1 gem installed
    ➜  ~/D/w/t/hello-minitest  tree .
    .
    ├── app.rb
    ├── foo.rb
    └── test
        └── test_foo.rb
    

    写 foo.rb:

    class Foo
      def bar
        p "Hello world!"
      end
    end
    

    编写测试:

    require 'minitest/autorun'
    require_relative '../foo.rb'
    
    class TestFoo < Minitest::Test
      def test_it_works
        assert_equal 1, 1
      end
    
      def test_bar
        assert_equal Foo.new.bar, "Hello world!"
      end
    end
    

    像 ruby​​ 脚本一样运行测试:

    ➜  ~/D/w/t/hello-minitest  ruby test/test_foo.rb
    Run options: --seed 56777
    
    # Running:
    
    "Hello world!"
    ..
    
    Finished in 0.001085s, 1843.3179 runs/s, 1843.3179 assertions/s.
    
    2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
    

    【讨论】:

      猜你喜欢
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 2013-07-31
      • 1970-01-01
      相关资源
      最近更新 更多