2016-11-08 46 views
-1

考虑到以下目录结构: “Hello World” 的世界,你好MINITEST CLI例如

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

1 directory, 3 files 

如果我跑app.rb我得到的经典响应。

➜ ~/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 

我将如何将测试我Foo类从命令行MINITEST?

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

你会爱你:)

回答

0

你可以像ruby文件一样运行minitest测试。只要安装MINITEST,写一个测试,并运行像使用Ruby脚本:

安装MINITEST:

➜ ~/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 

运行测试像红宝石脚本:

➜ ~/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