More Related Content Similar to Ruby初級者向けレッスン 第46回 ─── Test::Unit Similar to Ruby初級者向けレッスン 第46回 ─── Test::Unit (20) Ruby初級者向けレッスン 第46回 ─── Test::Unit13. Step 0 テスト
require ’test/unit’
require ’./myarray’
class TestMyArray Test::Unit::TestCase
def setup
@array = MyArray.new
end
def test_empty_by_empty_myarray
assert(@array.empty?)
end
end
15. Step 0 ERROR 実行
$ ruby test_myarray.rb
...
E
===================================================
Error: test_empty_by_empty_myarray(TestMyArray)
NoMethodError: undefined method ‘empty?’ for
#MyArray:0x802ea72c @array=[]
test_myarray.rb:12:in ‘test_empty_by_empty_myarray’
...
1 tests, 0 assertions, 0 failures, 1 errors,
0 pendings, 0 omissions, 0 notifications
16. Step 1 プロダクト
class MyArray
def initialize
@array = []
end
def empty? # = 追加
end # = 追加
end
17. Step 1 Red 実行
$ ruby test_myarray.rb
...
F
===================================================
Failure: nil is not true.
test_empty_by_empty_myarray(TestMyArray)
test_myarray.rb:12:in ‘test_empty_by_empty_myarray’
...
1 tests, 1 assertions, 1 failures, 0 errors,
0 pendings, 0 omissions, 0 notifications
18. Step 2 プロダクト
class MyArray
def initialize
@array = []
end
def empty?
true # = 追加
end
end
19. Step 2 Fake it! 実行
$ ruby test_myarray.rb
Loaded suite test_myarray
Started
.
Finished in 0.000942 seconds.
1 tests, 1 assertions, 0 failures, 0 errors,
0 pendings, 0 omissions, 0 notifications
100% passed
21. assert equal
第1引数 == 第2引数 なら、テストが通る。
assert_equal(0, array.size)
assert_equal(’matz’, author)
assert_equal(0.3, 0.2 + 0.1, Fail!!)
Fail!!
0.3 expected but was
0.30000000000000004
24. Step 3 プロダクト
class MyArray
...
def empty?
true # = Fake it!
end
def o # = 追加
@array o # = 追加
self # = 追加
end # = 追加
end
25. Step 3 テスト
...
class TestMyArray Test::Unit::TestCase
...
def test_empty_by_empty_myarray
assert(@array.empty?)
end
def test_empty_by_nonempty_myarray # = 追加
@array 1st # = 追加
assert(!@array.empty?) # = 追加
end # = 追加
end
26. Step 3 Triangulate 実行
$ ruby test_myarray.rb
...
.F
===================================================
Failure: false is not true.
test_empty_by_nonempty_myarray(TestMyArray)
test_myarray.rb:17:in ‘test_empty_by_nonempty_myarra
...
2 tests, 2 assertions, 1 failures, 0 errors,
0 pendings, 0 omissions, 0 notifications
27. Step 4 プロダクト
class MyArray
...
def empty?
@array.empty? # = 修正
end
def o
@array o
self
end
end
28. Step 4 Green 実行
$ ruby test_myarray.rb
Loaded suite test_myarray
Started
..
Finished in 0.00123 seconds.
2 tests, 2 assertions, 0 failures, 0 errors,
0 pendings, 0 omissions, 0 notifications
100% passed
29. 小技 --help
$ ruby test_myarray.rb --help
Test::Unit automatic runner.
Usage: test_myarray.rb [options] [-- untouched argum
-r, --runner=RUNNER Use the given RUNNE
--collector=COLLECTOR Use the given COLLE
--ignore-name=NAME Ignores tests match
-t, --testcase=TESTCASE Runs tests in TestC
...
-h, --help Display this help.
Deprecated options:
--console Console runner (use
30. 小技 --verbose
$ ruby test_myarray.rb --verbose
Loaded suite test_myarray
Started
TestMyArray:
test_empty_by_empty_myarray: .: (0.000630)
test_empty_by_nonempty_myarray: .: (0.000499)
Finished in 0.001676 seconds.
2 tests, 2 assertions, 0 failures, 0 errors,
0 pendings, 0 omissions, 0 notifications
100% passed
31. 小技 テストの指定
$ ruby test_myarray.rb -v -n /_non/
Loaded suite test_myarray
Started
TestMyArray:
test_empty_by_nonempty_myarray: .: (0.000499)
Finished in 0.000892 seconds.
1 tests, 1 assertions, 0 failures, 0 errors,
0 pendings, 0 omissions, 0 notifications
100% passed
33. Test Double
• テスト対象 (プロダクト) が依存している
コンポーネントを置き換える
– ダミーオブジェクト
– テストスタブ
– テストスパイ
– モックオブジェクト
– フェイクオブジェクト
http://goyoki.hatenablog.com/entry/
20120301/1330608789
35. もしくは Mocha を使う
require ’mocha/setup’
class TestPerson Test::Unit::TestCase
def test_age
matz = Person.new(
’matz’, Time.local(1965, 4, 14))
Time.stubs(:now)
.returns(Time.local(2013, 6, 15))
assert_equal(48, matz.age)
end
end
36. あるいは RR を使う
require ’rr’
class TestPerson Test::Unit::TestCase
def test_age
matz = Person.new(
’matz’, Time.local(1965, 4, 14))
mock(Time).now{Time.local(2013, 6, 15)}
assert_equal(48, matz.age)
end
end
39. 演習問題 1
• スタッククラスを作ろう!
– テスト駆動開発で「スタック」クラスを作っ
てみよう。
– push で順に要素を追加できること。
– pop で最後から順に要素を取り出せること。
– このような動作を LIFO (Last In, First
Out) という。
41. Step 1 empty?
• 新しいスタックの empty? は真であること。
• テストが失敗することを確認しよう。Red
• Stack#empty? で真を返そう。Fake it!
42. Step 2 push pop
• 新しいスタックに 3 を push して、
pop すると 3が返ること。
• テストが失敗することを確認しよう。Red
• pop で 3 を返そう。Fake it!
43. Step 3 size
• 新しいスタックに 3 を push すると
size が 1 になること。
• テストが失敗することを確認しよう。Red
• Stack#size で 1 を返そう。Fake it!
44. Step 4 size
• 新しいスタックに 3 を push した後、
5 を push すると size が 2 になること。
• Fake it! が通らなくなることを確認しよう。
Triangulate
• テストが通るように修正しよう。
45. Step 5 empty?
• 新しいスタックに 3 を push すると
empty? は偽であること。
• Fake it! が通らなくなることを確認しよう。
Triangulate
• テストが通るように修正しよう。
46. Step 6 pop
• 新しいスタックから pop すると
Stack::EmptyStackErrorが発生すること。
47. Step 7 pop
• 新しいスタックに 3 を push した後、5 を
push し、pop すると size が 1 であること。
48. Step 8 pop
• 新しいスタックに 3 を push した後、
5 を push し、pop すると 5 が返ること。