PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/test/samples_test.rb

http://github.com/jimweirich/flexmock
Ruby | 283 lines | 204 code | 50 blank | 29 comment | 1 complexity | 9de7f5f975befe9f1cfc481987a065e1 MD5 | raw file
  1. #!/usr/bin/env ruby
  2. #---
  3. # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
  4. # All rights reserved.
  5. # Permission is granted for use, copying, modification, distribution,
  6. # and distribution of modified versions of this work as long as the
  7. # above copyright notice is included.
  8. #+++
  9. require 'test/test_setup'
  10. # Sample FlexMock Usage.
  11. class TestSamples < Test::Unit::TestCase
  12. include FlexMock::TestCase
  13. # This is a basic example where we setup a mock object to mimic an
  14. # IO object. We know that the +count_lines+ method uses gets, so we
  15. # tell the mock object to handle +gets+ by returning successive
  16. # elements of an array (just as the real +gets+ returns successive
  17. # elements of a file.
  18. def test_file_io
  19. mock_file = flexmock("file")
  20. mock_file.should_receive(:gets).and_return("line 1", "line 2", nil)
  21. assert_equal 2, count_lines(mock_file)
  22. end
  23. # Count the number of lines in a file. Used in the test_file_io
  24. # test.
  25. def count_lines(file)
  26. n = 0
  27. while file.gets
  28. n += 1
  29. end
  30. n
  31. end
  32. end
  33. class TestUndefined < Test::Unit::TestCase
  34. include FlexMock::TestCase
  35. def test_undefined_values
  36. m = flexmock("mock")
  37. m.should_receive(:divide_by).with(0).
  38. and_return_undefined
  39. assert_equal FlexMock.undefined, m.divide_by(0)
  40. end
  41. end
  42. class TestSimple < Test::Unit::TestCase
  43. include FlexMock::TestCase
  44. def test_simple_mock
  45. m = flexmock(:pi => 3.1416, :e => 2.71)
  46. assert_equal 3.1416, m.pi
  47. assert_equal 2.71, m.e
  48. end
  49. end
  50. class TestDog < Test::Unit::TestCase
  51. include FlexMock::TestCase
  52. def test_dog_wags
  53. tail_mock = flexmock(:wag => :happy)
  54. assert_equal :happy, tail_mock.wag
  55. end
  56. end
  57. class Woofer
  58. end
  59. class Dog
  60. def initialize
  61. @woofer = Woofer.new
  62. end
  63. def bark
  64. @woofer.woof
  65. end
  66. def wag
  67. :happy
  68. end
  69. end
  70. class TestDogBarking < Test::Unit::TestCase
  71. include FlexMock::TestCase
  72. # Setup the tests by mocking the +new+ method of
  73. # Woofer and return a mock woofer.
  74. def setup
  75. @dog = Dog.new
  76. flexmock(@dog, :bark => :grrr)
  77. end
  78. def test_dog
  79. assert_equal :grrr, @dog.bark # Mocked Method
  80. assert_equal :happy, @dog.wag # Normal Method
  81. end
  82. end
  83. class TestDogBarkingWithNewInstances < Test::Unit::TestCase
  84. include FlexMock::TestCase
  85. # Setup the tests by mocking Woofer to always
  86. # return partial mocks.
  87. def setup
  88. flexmock(Woofer).new_instances.should_receive(:woof => :grrr)
  89. end
  90. def test_dog
  91. assert_equal :grrr, Dog.new.bark # All dog objects
  92. assert_equal :grrr, Dog.new.bark # are mocked.
  93. end
  94. end
  95. class TestDefaults < Test::Unit::TestCase
  96. include FlexMock::TestCase
  97. def setup
  98. @mock_dog = flexmock("Fido")
  99. @mock_dog.should_receive(:tail => :a_tail, :bark => "woof").by_default
  100. end
  101. def test_something_where_bark_must_be_called_once
  102. @mock_dog.should_receive(:bark => "bow wow").once
  103. assert_equal "bow wow", @mock_dog.bark
  104. assert_equal :a_tail, @mock_dog.tail
  105. end
  106. end
  107. class TestDemeter < Test::Unit::TestCase
  108. include FlexMock::TestCase
  109. def test_manual_mocking
  110. # Manually mocking a Law of Demeter violation
  111. cog = flexmock("cog")
  112. cog.should_receive(:turn).once.and_return(:ok)
  113. joint = flexmock("gear", :cog => cog)
  114. axle = flexmock("axle", :universal_joint => joint)
  115. chassis = flexmock("chassis", :axle => axle)
  116. car = flexmock("car", :chassis => chassis)
  117. # test code
  118. assert_equal :ok, car.chassis.axle.universal_joint.cog.turn
  119. end
  120. def test_demeter
  121. car = flexmock("car")
  122. car.should_receive( "chassis.axle.universal_joint.cog.turn" => :ok).once
  123. # Test code
  124. assert_equal :ok, car.chassis.axle.universal_joint.cog.turn
  125. end
  126. end
  127. class TestDb < Test::Unit::TestCase
  128. include FlexMock::TestCase
  129. def test_db
  130. db = flexmock('db')
  131. db.should_receive(:query).and_return([1,2,3])
  132. db.should_receive(:update).with(5).and_return(nil).once
  133. # test code
  134. assert_nil db.update(5)
  135. end
  136. end
  137. class TestDb < Test::Unit::TestCase
  138. include FlexMock::TestCase
  139. def test_query_and_update
  140. db = flexmock('db')
  141. db.should_receive(:query).and_return([1,2,3]).ordered
  142. db.should_receive(:update).and_return(nil).ordered
  143. # test code here
  144. assert_raises(assertion_failed_error) do
  145. db.update
  146. db.query
  147. end
  148. end
  149. def test_ordered_queries
  150. db = flexmock('db')
  151. db.should_receive(:startup).once.ordered
  152. db.should_receive(:query).with("CPWR").and_return(12.3).
  153. once.ordered(:queries)
  154. db.should_receive(:query).with("MSFT").and_return(10.0).
  155. once.ordered(:queries)
  156. db.should_receive(:query).with(/^....$/).and_return(3.3).
  157. at_least.once.ordered(:queries)
  158. db.should_receive(:finish).once.ordered
  159. # test code here
  160. db.startup
  161. db.query("CPWR")
  162. db.query("MSFT")
  163. db.query("asdf")
  164. db.finish
  165. end
  166. def test_ordered_queries_in_record_mode
  167. db = flexmock('db')
  168. db.should_expect do |rec|
  169. rec.startup.once.ordered
  170. rec.query("CPWR") { 12.3 }.once.ordered(:queries)
  171. rec.query("MSFT") { 10.0 }.once.ordered(:queries)
  172. rec.query(/^....$/) { 3.3 }.at_least.once.ordered(:queries)
  173. rec.finish.once.ordered
  174. end
  175. # test code here using +db+.
  176. db.startup
  177. db.query("CPWR")
  178. db.query("MSFT")
  179. db.query("asdf")
  180. db.finish
  181. end
  182. def known_good_way_to_build_xml(builder)
  183. builder.html
  184. end
  185. def new_way_to_build_xml(builder)
  186. known_good_way_to_build_xml(builder)
  187. end
  188. def test_build_xml
  189. builder = flexmock('builder')
  190. builder.should_expect do |rec|
  191. rec.should_be_strict
  192. known_good_way_to_build_xml(rec) # record the messages
  193. end
  194. new_way_to_build_xml(builder) # compare to new way
  195. end
  196. end
  197. class TestMoreSamples < Test::Unit::TestCase
  198. include FlexMock::TestCase
  199. def test_multiple_gets
  200. file = flexmock('file')
  201. file.should_receive(:gets).with_no_args.
  202. and_return("line 1\n", "line 2\n")
  203. # test code here
  204. assert_equal "line 1\n", file.gets
  205. assert_equal "line 2\n", file.gets
  206. end
  207. def test_an_important_message
  208. m = flexmock('m')
  209. m.should_receive(:an_important_message).and_return(1).once
  210. m.should_ignore_missing
  211. # test code here
  212. assert_equal 1, m.an_important_message
  213. assert_equal FlexMock.undefined, m.other
  214. end
  215. class QuoteService
  216. end
  217. class Portfolio
  218. def initialize
  219. @quote_service = QuoteService.new
  220. end
  221. def value
  222. @quote_service.quote
  223. end
  224. end
  225. def test_portfolio_value
  226. flexmock(QuoteService).new_instances do |m|
  227. m.should_receive(:quote).and_return(100)
  228. end
  229. port = Portfolio.new
  230. value = port.value # Portfolio calls QuoteService.quote
  231. assert_equal 100, value
  232. end
  233. end