PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/examples/test_unit_examples_test.rb

http://github.com/jimweirich/flexmock
Ruby | 240 lines | 121 code | 61 blank | 58 comment | 0 complexity | cd4d246f3ae7514fc915bf5b528ece13 MD5 | raw file
  1. require 'flexmock/test_unit'
  2. class TestSimple < Test::Unit::TestCase
  3. # Simple stubbing of some methods
  4. def test_simple_mock
  5. m = flexmock(:pi => 3.1416, :e => 2.71)
  6. assert_equal 3.1416, m.pi
  7. assert_equal 2.71, m.e
  8. end
  9. end
  10. class TestUndefined < Test::Unit::TestCase
  11. # Create a mock object that returns an undefined object for method calls
  12. def test_undefined_values
  13. m = flexmock("mock")
  14. m.should_receive(:divide_by).with(0).
  15. and_return_undefined
  16. assert_equal FlexMock.undefined, m.divide_by(0)
  17. end
  18. end
  19. class TestDb < Test::Unit::TestCase
  20. # Expect multiple queries and a single update
  21. # Multiple calls to the query method will be allows, and calls may
  22. # have any argument list. Each call to query will return the three
  23. # element array [1, 2, 3]. The call to update must have a specific
  24. # argument of 5.
  25. def test_db
  26. db = flexmock('db')
  27. db.should_receive(:query).and_return([1,2,3])
  28. db.should_receive(:update).with(5).and_return(nil).once
  29. # Test Code
  30. db.query
  31. db.update(5)
  32. end
  33. end
  34. class TestOrdered < Test::Unit::TestCase
  35. # Expect all queries before any updates
  36. # All the query message must occur before any of the update
  37. # messages.
  38. def test_query_and_update
  39. db = flexmock('db')
  40. db.should_receive(:query).and_return([1,2,3]).ordered
  41. db.should_receive(:update).and_return(nil).ordered
  42. # test code here
  43. db.query
  44. db.update
  45. end
  46. end
  47. class MoreOrdered < Test::Unit::TestCase
  48. # Expect several queries with different parameters
  49. # The queries should happen after startup but before finish. The
  50. # queries themselves may happen in any order (because they are in
  51. # the same order group). The first two queries should happen exactly
  52. # once, but the third query (which matches any query call with a
  53. # four character parameter) may be called multiple times (but at
  54. # least once). Startup and finish must also happen exactly once.
  55. # Also note that we use the <code>with</code> method to match
  56. # different argument values to figure out what value to return.
  57. def test_ordered_queries
  58. db = flexmock('db')
  59. db.should_receive(:startup).once.ordered
  60. db.should_receive(:query).with("CPWR").and_return(12.3).
  61. once.ordered(:queries)
  62. db.should_receive(:query).with("MSFT").and_return(10.0).
  63. once.ordered(:queries)
  64. db.should_receive(:query).with(/^....$/).and_return(3.3).
  65. at_least.once.ordered(:queries)
  66. db.should_receive(:finish).once.ordered
  67. # Test Code
  68. db.startup
  69. db.query("MSFT")
  70. db.query("XYZY")
  71. db.query("CPWR")
  72. db.finish
  73. end
  74. end
  75. class EvenMoreOrderedTest < Test::Unit::TestCase
  76. # Same as above, but using the Record Mode interface
  77. # The record mode interface offers much the same features as the
  78. # <code>should_receive</code> interface introduced so far, but it
  79. # allows the messages to be sent directly to a recording object
  80. # rather than be specified indirectly using a symbol.
  81. def test_ordered_queries_in_record_mode
  82. db = flexmock('db')
  83. db.should_expect do |rec|
  84. rec.startup.once.ordered
  85. rec.query("CPWR") { 12.3 }.once.ordered(:queries)
  86. rec.query("MSFT") { 10.0 }.once.ordered(:queries)
  87. rec.query(/^....$/) { 3.3 }.at_least.once.ordered(:queries)
  88. rec.finish.once.ordered
  89. end
  90. # Test Code
  91. db.startup
  92. db.query("MSFT")
  93. db.query("XYZY")
  94. db.query("CPWR")
  95. db.finish
  96. end
  97. end
  98. class RecordedTest < Test::Unit::TestCase
  99. # Using Record Mode to record a known, good algorithm for testing
  100. # Record mode is nice when you have a known, good algorithm that can
  101. # use a recording mock object to record the steps. Then you compare
  102. # the execution of a new algorithm to behavior of the old using the
  103. # recorded expectations in the mock. For this you probably want to
  104. # put the recorder in _strict_ mode so that the recorded
  105. # expectations use exact matching on argument lists, and strict
  106. # ordering of the method calls.
  107. # <b>Note:</b> This is most useful when there are no queries on the
  108. # mock objects, because the query responses cannot be programmed
  109. # into the recorder object.
  110. def test_build_xml
  111. builder = flexmock('builder')
  112. builder.should_expect do |rec|
  113. rec.should_be_strict
  114. known_good_way_to_build_xml(rec) # record the messages
  115. end
  116. new_way_to_build_xml(builder) # compare to new way
  117. end
  118. def known_good_way_to_build_xml(builder)
  119. builder.person
  120. end
  121. def new_way_to_build_xml(builder)
  122. builder.person
  123. end
  124. end
  125. class MultipleReturnValueTest < Test::Unit::TestCase
  126. # Expect multiple calls, returning a different value each time
  127. # Sometimes you need to return different values for each call to a
  128. # mocked method. This example shifts values out of a list for this
  129. # effect.
  130. def test_multiple_gets
  131. file = flexmock('file')
  132. file.should_receive(:gets).with_no_args.
  133. and_return("line 1\n", "line 2\n")
  134. # test code here
  135. file.gets # returns "line 1"
  136. file.gets # returns "line 2"
  137. end
  138. end
  139. class IgnoreUnimportantMessages < Test::Unit::TestCase
  140. # Ignore uninteresting messages
  141. # Generally you need to mock only those methods that return an
  142. # interesting value or wish to assert were sent in a particular
  143. # manner. Use the <code>should_ignore_missing</code> method to turn
  144. # on missing method ignoring.
  145. def test_an_important_message
  146. m = flexmock('m')
  147. m.should_receive(:an_important_message).and_return(1).once
  148. m.should_ignore_missing
  149. # Test Code
  150. m.an_important_message
  151. m.an_unimportant_message
  152. end
  153. # When <code>should_ignore_missing</code> is enabled, ignored
  154. # missing methods will return an undefined object. Any operation on
  155. # the undefined object will return the undefined object.
  156. end
  157. class PartialMockTest < Test::Unit::TestCase
  158. # Mock just one method on an existing object
  159. # The Portfolio class calculate the value of a set of stocks by
  160. # talking to a quote service via a web service. Since we don't want
  161. # to use a real web service in our unit tests, we will mock the
  162. # quote service.
  163. def test_portfolio_value
  164. flexmock(QuoteService).new_instances do |m|
  165. m.should_receive(:quote).and_return(100)
  166. end
  167. port = Portfolio.new
  168. value = port.value # Portfolio calls QuoteService.quote
  169. assert_equal 100, value
  170. end
  171. class QuoteService
  172. end
  173. class Portfolio
  174. def value
  175. qs = QuoteService.new
  176. qs.quote
  177. end
  178. end
  179. end