3. MockとStubとは
Test Doubleという概念の一部。
Double とは、代役のことで、テスト用にオブジェクトを
入れ替えるときに一般的に用いられる言葉。
[1] xUnit Test Patterns by Gerard Meszaros
http://xunitpatterns.com/Test%20Double.html
25. Mock サンプルコード on RR
どういう意味でしょうか?
意味でしょうか
Q. どういう意味でしょうか?
view = View.new
mock(view).render(:partial => "user_info") {"Information"}
次の方法でモックへ渡される引数をいくつでも許せます。
方法でモックへ渡される引数をいくつでも許せます。
でモックへ 引数をいくつでも
mock(view).render.with_any_args.twice do |*args|
if args.first == {:partial => "user_info}
"User Info"
else
"Stuff in the view #{args.inspect}"
end
end
26. Stub サンプルコード on RR
どういう意味でしょうか?
意味でしょうか
Q. どういう意味でしょうか?
jane = User.new('Jane')
bob = User.new('Bob')
stub(User).find('42') {jane}
stub(User).find('99') {bob}
stub(User).find do |id|
raise "Unexpected id #{id.inspect} passed to me"
end
28. mock.proxy サンプルコード
返り値を置き換えない mock
class Test
def method_a
“a”
end
end
test = Test.new
mock.proxy(test).method_a # expects once called
puts test.method_a # a
# normal mock
mock(test).method_a # expects once called
puts test.method_a # 空
実際の処理をさせ、
実際の処理をさせ、返り値を受け取り、偽装して返す。
をさせ 偽装して返
して
view = controller.template
mock.proxy(view).render(:partial => "user_info") do |html|
html.should include("John Doe")
"Different html"
end
29. stub.proxy サンプルコード
返り値をインターセプト
view = controller.template
stub.proxy(view).render(:partial => "user_info") do |html|
html.should include("Joe Smith")
html
end
36. 参考文献
[1] Test Double at XUnitPatterns.com
http://xunitpatterns.com/Test%20Double.html
[2] テストダブル
http://capsctrl.que.jp/kdmsnr/wiki/bliki/?TestDouble
[3] ricollab Web Tech Blog » Blog Archive » Mock と Stub について
http://blogs.ricollab.jp/webtech/2009/09/mock_and_stub/
[4] brian - Introducing RR
http://pivotallabs.com/users/brian/blog/articles/352-
introducing-rr
[5] An Introduction to Mock Objects in Ruby by James Mead
http://jamesmead.org/talks/2007-07-09-introduction-to-mock-
objects-in-ruby-at-lrug/
[6] RR README.doc
https://gist.github.com/330284/fb3fdbf2dd91a800ed83de37b8d
15ea84c3dd2c8