Last active
August 29, 2015 14:15
-
-
Save thash/5868eef6a36e638747af to your computer and use it in GitHub Desktop.
AllAbout 029 minitest/TDD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'bundler/setup' | |
| require 'socket' | |
| require 'minitest/autorun' | |
| class AssertSamples < Minitest::Test | |
| def test_asserts | |
| # 等しいこと | |
| assert_equal 5, 2 + 3 | |
| # 空(empty?がtrue)であること | |
| assert_empty [] | |
| # 要素が含まれること | |
| assert_includes 1..100, 55 | |
| # 正規表現にマッチすること | |
| assert_match(/\d{4}-\d{2}-\d{2}/, '2015-02-20') | |
| # nilであること | |
| assert_nil @my_undefined | |
| # あるクラスのインスタンスであること | |
| assert_instance_of Date, Date.parse('2015-02-20') | |
| # ブロック内のコードが特定の例外を発生させること | |
| assert_raises SocketError do | |
| TCPSocket.new 'no-such-hostname.example.com', 888 | |
| end | |
| # エラーが起こらないこと | |
| assert_silent do | |
| TCPSocket.new 'httpbin.org', 80 | |
| end | |
| # 標準出力(あるいはエラー出力)にある文字列が出ること | |
| assert_output "egho\n" do | |
| puts %w(h o g e).sort.join | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # require 'minitest/autorun' | |
| require 'minitest' | |
| # ... テストケースの記述 | |
| Minitest.autorun |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'bundler/setup' | |
| require 'minitest/autorun' | |
| def fibonacci(n) | |
| end | |
| class TestFibonacci < Minitest::Test | |
| def test_fibonacci | |
| assert_equal 0, fibonacci(0) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'bundler/setup' | |
| require 'minitest/autorun' | |
| def fibonacci(n) | |
| end | |
| class TestFibonacci < Minitest::Test | |
| def test_fibonacci | |
| assert_equal 0, fibonacci(0) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Run options: --seed 40867 | |
| # Running: | |
| F | |
| Finished in 0.000895s, 1117.3184 runs/s, 1117.3184 assertions/s. | |
| 1) Failure: | |
| TestFibonacci#test_fibonacci [fib1.rb:9]: | |
| Expected: 0 | |
| Actual: nil | |
| 1 runs, 1 assertions, 1 failures, 0 errors, 0 skips |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fibonacci(n) | |
| 0 | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Finished in 0.000979s, 1021.4505 runs/s, 1021.4505 assertions/s. | |
| 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # n = 1のテストを追加し... | |
| assert_equal 1, fibonacci(1) | |
| # 0, 1の両方で通るように修正します | |
| def fibonacci(n) | |
| return 0 if n == 0 | |
| return 1 if n == 1 | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fibonacci(n) | |
| return 0 if n == 0 | |
| prev, current = 0, 1 | |
| 2.upto(n) do | |
| prev, current = current, prev + current | |
| end | |
| current | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Finished in 0.001011s, 1978.2394 runs/s, 8902.0772 assertions/s. | |
| 2 runs, 9 assertions, 0 failures, 0 errors, 0 skips |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fibonacci(n) | |
| return 0 if n == 0 | |
| return 1 if n == 1 | |
| fibonacci(n - 1) + fibonacci(n - 2) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TestFibonacci < Minitest::Test | |
| def test_fibonacci_init | |
| assert_equal 0, fibonacci(0) | |
| assert_equal 1, fibonacci(1) | |
| end | |
| def test_fibonacci_n | |
| assert_equal 1, fibonacci(2) | |
| assert_equal 2, fibonacci(3) | |
| assert_equal 3, fibonacci(4) | |
| assert_equal 5, fibonacci(5) | |
| assert_equal 8, fibonacci(6) | |
| assert_equal 55, fibonacci(10) | |
| assert_equal 102334155, fibonacci(40) | |
| end | |
| end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'bundler/setup' | |
| require 'minitest/autorun' | |
| class MyClass | |
| def add(x, y) | |
| x + y | |
| end | |
| end | |
| # Minitest::Testを継承 | |
| class TestMyClass < Minitest::Test | |
| def setup | |
| @my = MyClass.new | |
| end | |
| # test_* にマッチするメソッドを定義 | |
| def test_add | |
| assert_equal 5, @my.add(2, 3) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ ruby first.rb | |
| Run options: --seed 14863 | |
| # Running: | |
| . | |
| Finished in 0.001392s, 718.3908 runs/s, 718.3908 assertions/s. | |
| 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'bundler/setup' | |
| require 'minitest/autorun' | |
| class RefuteSamples < Minitest::Test | |
| def test_refutes | |
| refute_equal 6, 2 + 3 | |
| refute_empty [1, 2, 3] | |
| refute_includes 1..100, 550 | |
| refute_match(/\d{4}-\d{2}-\d{2}/, '2015-2-20') | |
| refute_nil 123 | |
| refute_instance_of Time, Date.parse('2015-02-20') | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'bundler/setup' | |
| require 'minitest/autorun' | |
| class MySpec | |
| def mul(x, y) | |
| x * y | |
| end | |
| end | |
| describe MySpec do | |
| before do | |
| @my = MySpec.new | |
| end | |
| it { @my.mul(2, 3).must_equal 6 } | |
| it '説明テキスト' do | |
| @my.mul(2, 10).must_equal 20 | |
| end | |
| # 実はassertで書いても大丈夫 | |
| it { assert_equal 6, @my.mul(2, 3) } | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment