PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/expectation_test.rb

https://github.com/sander6/mocha
Ruby | 507 lines | 414 code | 93 blank | 0 comment | 3 complexity | e2cf51d33c0d39442babb44dfd0db22d MD5 | raw file
  1. require File.expand_path('../../test_helper', __FILE__)
  2. require 'method_definer'
  3. require 'mocha/expectation'
  4. require 'mocha/sequence'
  5. require 'execution_point'
  6. require 'simple_counter'
  7. class ExpectationTest < Test::Unit::TestCase
  8. include Mocha
  9. def new_expectation
  10. Expectation.new(nil, :expected_method)
  11. end
  12. def test_should_match_calls_to_same_method_with_any_parameters
  13. assert new_expectation.match?(:expected_method, 1, 2, 3)
  14. end
  15. def test_should_match_calls_to_same_method_with_exactly_zero_parameters
  16. expectation = new_expectation.with()
  17. assert expectation.match?(:expected_method)
  18. end
  19. def test_should_not_match_calls_to_same_method_with_more_than_zero_parameters
  20. expectation = new_expectation.with()
  21. assert !expectation.match?(:expected_method, 1, 2, 3)
  22. end
  23. def test_should_match_calls_to_same_method_with_expected_parameter_values
  24. expectation = new_expectation.with(1, 2, 3)
  25. assert expectation.match?(:expected_method, 1, 2, 3)
  26. end
  27. def test_should_match_calls_to_same_method_with_parameters_constrained_as_expected
  28. expectation = new_expectation.with() {|x, y, z| x + y == z}
  29. assert expectation.match?(:expected_method, 1, 2, 3)
  30. end
  31. def test_should_not_match_calls_to_different_method_with_parameters_constrained_as_expected
  32. expectation = new_expectation.with() {|x, y, z| x + y == z}
  33. assert !expectation.match?(:different_method, 1, 2, 3)
  34. end
  35. def test_should_not_match_calls_to_different_methods_with_no_parameters
  36. assert !new_expectation.match?(:unexpected_method)
  37. end
  38. def test_should_not_match_calls_to_same_method_with_too_few_parameters
  39. expectation = new_expectation.with(1, 2, 3)
  40. assert !expectation.match?(:unexpected_method, 1, 2)
  41. end
  42. def test_should_not_match_calls_to_same_method_with_too_many_parameters
  43. expectation = new_expectation.with(1, 2)
  44. assert !expectation.match?(:unexpected_method, 1, 2, 3)
  45. end
  46. def test_should_not_match_calls_to_same_method_with_unexpected_parameter_values
  47. expectation = new_expectation.with(1, 2, 3)
  48. assert !expectation.match?(:unexpected_method, 1, 0, 3)
  49. end
  50. def test_should_not_match_calls_to_same_method_with_parameters_not_constrained_as_expected
  51. expectation = new_expectation.with() {|x, y, z| x + y == z}
  52. assert !expectation.match?(:expected_method, 1, 0, 3)
  53. end
  54. def test_should_allow_invocations_until_expected_invocation_count_is_one_and_actual_invocation_count_would_be_two
  55. expectation = new_expectation.times(1)
  56. assert expectation.invocations_allowed?
  57. expectation.invoke
  58. assert !expectation.invocations_allowed?
  59. end
  60. def test_should_allow_invocations_until_expected_invocation_count_is_two_and_actual_invocation_count_would_be_three
  61. expectation = new_expectation.times(2)
  62. assert expectation.invocations_allowed?
  63. expectation.invoke
  64. assert expectation.invocations_allowed?
  65. expectation.invoke
  66. assert !expectation.invocations_allowed?
  67. end
  68. def test_should_allow_invocations_until_expected_invocation_count_is_a_range_from_two_to_three_and_actual_invocation_count_would_be_four
  69. expectation = new_expectation.times(2..3)
  70. assert expectation.invocations_allowed?
  71. expectation.invoke
  72. assert expectation.invocations_allowed?
  73. expectation.invoke
  74. assert expectation.invocations_allowed?
  75. expectation.invoke
  76. assert !expectation.invocations_allowed?
  77. end
  78. def test_should_store_provided_backtrace
  79. backtrace = Object.new
  80. expectation = Expectation.new(nil, :expected_method, backtrace)
  81. assert_equal backtrace, expectation.backtrace
  82. end
  83. def test_should_default_backtrace_to_caller
  84. execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
  85. assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
  86. end
  87. def test_should_not_yield
  88. yielded = false
  89. new_expectation.invoke() { yielded = true }
  90. assert_equal false, yielded
  91. end
  92. def test_should_yield_no_parameters
  93. expectation = new_expectation().yields()
  94. yielded_parameters = nil
  95. expectation.invoke() { |*parameters| yielded_parameters = parameters }
  96. assert_equal Array.new, yielded_parameters
  97. end
  98. def test_should_yield_with_specified_parameters
  99. expectation = new_expectation().yields(1, 2, 3)
  100. yielded_parameters = nil
  101. expectation.invoke() { |*parameters| yielded_parameters = parameters }
  102. assert_equal [1, 2, 3], yielded_parameters
  103. end
  104. def test_should_yield_different_parameters_on_consecutive_invocations
  105. expectation = new_expectation().yields(1, 2, 3).yields(4, 5)
  106. yielded_parameters = []
  107. expectation.invoke() { |*parameters| yielded_parameters << parameters }
  108. expectation.invoke() { |*parameters| yielded_parameters << parameters }
  109. assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
  110. end
  111. def test_should_yield_multiple_times_for_single_invocation
  112. expectation = new_expectation().multiple_yields([1, 2, 3], [4, 5])
  113. yielded_parameters = []
  114. expectation.invoke() { |*parameters| yielded_parameters << parameters }
  115. assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
  116. end
  117. def test_should_yield_multiple_times_for_first_invocation_and_once_for_second_invocation
  118. expectation = new_expectation().multiple_yields([1, 2, 3], [4, 5]).then.yields(6, 7)
  119. yielded_parameters = []
  120. expectation.invoke() { |*parameters| yielded_parameters << parameters }
  121. expectation.invoke() { |*parameters| yielded_parameters << parameters }
  122. assert_equal [[1, 2, 3], [4, 5], [6, 7]], yielded_parameters
  123. end
  124. def test_should_return_specified_value
  125. expectation = new_expectation.returns(99)
  126. assert_equal 99, expectation.invoke
  127. end
  128. def test_should_return_result_of_trivial_block
  129. expectation = new_expectation.returns { 99 }
  130. assert_equal 99, expectation.invoke
  131. end
  132. def test_should_return_result_of_block_given_invocation_parameters
  133. expectation = new_expectation.returns { |x| x + 1 }
  134. assert_equal 100, expectation.invoke(99)
  135. end
  136. def test_should_return_same_specified_value_multiple_times
  137. expectation = new_expectation.returns(99)
  138. assert_equal 99, expectation.invoke
  139. assert_equal 99, expectation.invoke
  140. end
  141. def test_should_return_same_value_of_trivial_block_multiple_times
  142. expectation = new_expectation.returns { 99 }
  143. assert_equal 99, expectation.invoke
  144. assert_equal 99, expectation.invoke
  145. end
  146. def test_should_return_specified_values_on_consecutive_calls
  147. expectation = new_expectation.returns(99, 100, 101)
  148. assert_equal 99, expectation.invoke
  149. assert_equal 100, expectation.invoke
  150. assert_equal 101, expectation.invoke
  151. end
  152. def test_should_return_value_of_block_given_invocation_parameters_on_consecutive_calls
  153. expectation = new_expectation.returns { |x| x + 1 }
  154. assert_equal 100, expectation.invoke(99)
  155. assert_equal 101, expectation.invoke(100)
  156. assert_equal 102, expectation.invoke(101)
  157. end
  158. def test_should_return_specified_values_on_consecutive_calls_even_if_values_are_modified
  159. values = [99, 100, 101]
  160. expectation = new_expectation.returns(*values)
  161. values.shift
  162. assert_equal 99, expectation.invoke
  163. assert_equal 100, expectation.invoke
  164. assert_equal 101, expectation.invoke
  165. end
  166. def test_should_raise_ambiguous_return_error_if_given_a_block_and_static_return_values
  167. assert_raises(Mocha::AmbiguousReturnError) { new_expectation.returns(99) { |x| x + 1 } }
  168. end
  169. def test_should_return_nil_by_default
  170. assert_nil new_expectation.invoke
  171. end
  172. def test_should_return_nil_if_no_value_specified
  173. expectation = new_expectation.returns()
  174. assert_nil expectation.invoke
  175. end
  176. def test_should_raise_runtime_exception
  177. expectation = new_expectation.raises
  178. assert_raise(RuntimeError) { expectation.invoke }
  179. end
  180. def test_should_raise_custom_exception
  181. exception = Class.new(Exception)
  182. expectation = new_expectation.raises(exception)
  183. assert_raise(exception) { expectation.invoke }
  184. end
  185. def test_should_raise_same_instance_of_custom_exception
  186. exception_klass = Class.new(StandardError)
  187. expected_exception = exception_klass.new
  188. expectation = new_expectation.raises(expected_exception)
  189. actual_exception = assert_raise(exception_klass) { expectation.invoke }
  190. assert_same expected_exception, actual_exception
  191. end
  192. def test_should_use_the_default_exception_message
  193. expectation = new_expectation.raises(Exception)
  194. exception = assert_raise(Exception) { expectation.invoke }
  195. assert_equal Exception.new.message, exception.message
  196. end
  197. def test_should_raise_custom_exception_with_message
  198. exception_msg = "exception message"
  199. expectation = new_expectation.raises(Exception, exception_msg)
  200. exception = assert_raise(Exception) { expectation.invoke }
  201. assert_equal exception_msg, exception.message
  202. end
  203. def test_should_return_values_then_raise_exception
  204. expectation = new_expectation.returns(1, 2).then.raises()
  205. assert_equal 1, expectation.invoke
  206. assert_equal 2, expectation.invoke
  207. assert_raise(RuntimeError) { expectation.invoke }
  208. end
  209. def test_should_raise_exception_then_return_values
  210. expectation = new_expectation.raises().then.returns(1, 2)
  211. assert_raise(RuntimeError) { expectation.invoke }
  212. assert_equal 1, expectation.invoke
  213. assert_equal 2, expectation.invoke
  214. end
  215. def test_should_verify_successfully_if_expected_call_was_made
  216. expectation = new_expectation
  217. expectation.invoke
  218. assert expectation.verified?
  219. end
  220. def test_should_not_verify_successfully_if_call_expected_once_but_invoked_twice
  221. expectation = new_expectation.once
  222. expectation.invoke
  223. expectation.invoke
  224. assert !expectation.verified?
  225. end
  226. def test_should_not_verify_successfully_if_call_expected_once_but_not_invoked
  227. expectation = new_expectation.once
  228. assert !expectation.verified?
  229. end
  230. def test_should_verify_successfully_if_call_expected_once_and_invoked_once
  231. expectation = new_expectation.once
  232. expectation.invoke
  233. assert expectation.verified?
  234. end
  235. def test_should_not_verify_successfully_if_call_expected_twice_and_invoked_three_times
  236. expectation = new_expectation.twice
  237. expectation.invoke
  238. expectation.invoke
  239. expectation.invoke
  240. assert !expectation.verified?
  241. end
  242. def test_should_not_verify_successfully_if_call_expected_twice_but_invoked_once
  243. expectation = new_expectation.twice
  244. expectation.invoke
  245. assert !expectation.verified?
  246. end
  247. def test_should_verify_successfully_if_call_expected_twice_and_invoked_twice
  248. expectation = new_expectation.twice
  249. expectation.invoke
  250. expectation.invoke
  251. assert expectation.verified?
  252. end
  253. def test_should_verify_successfully_if_expected_call_was_made_at_least_once
  254. expectation = new_expectation.at_least_once
  255. 3.times {expectation.invoke}
  256. assert expectation.verified?
  257. end
  258. def test_should_not_verify_successfully_if_expected_call_was_not_made_at_least_once
  259. expectation = new_expectation.with(1, 2, 3).at_least_once
  260. assert !expectation.verified?
  261. assert_match(/expected at least once, not yet invoked/i, expectation.mocha_inspect)
  262. end
  263. def test_should_verify_successfully_if_expected_call_was_made_expected_number_of_times
  264. expectation = new_expectation.times(2)
  265. 2.times {expectation.invoke}
  266. assert expectation.verified?
  267. end
  268. def test_should_not_verify_successfully_if_expected_call_was_made_too_few_times
  269. expectation = new_expectation.times(2)
  270. 1.times {expectation.invoke}
  271. assert !expectation.verified?
  272. assert_match(/expected exactly twice, invoked once/i, expectation.mocha_inspect)
  273. end
  274. def test_should_not_verify_successfully_if_expected_call_was_made_too_many_times
  275. expectation = new_expectation.times(2)
  276. 3.times {expectation.invoke}
  277. assert !expectation.verified?
  278. end
  279. def test_should_increment_assertion_counter_for_expectation_because_it_does_need_verifyng
  280. expectation = new_expectation
  281. expectation.invoke
  282. assertion_counter = SimpleCounter.new
  283. expectation.verified?(assertion_counter)
  284. assert_equal 1, assertion_counter.count
  285. end
  286. def test_should_not_increment_assertion_counter_for_stub_because_it_does_not_need_verifying
  287. stub = Expectation.new(nil, :expected_method).at_least(0)
  288. assertion_counter = SimpleCounter.new
  289. stub.verified?(assertion_counter)
  290. assert_equal 0, assertion_counter.count
  291. end
  292. def test_should_store_backtrace_from_point_where_expectation_was_created
  293. execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
  294. assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
  295. end
  296. class FakeMock
  297. def initialize(name)
  298. @name = name
  299. end
  300. def mocha_inspect
  301. @name
  302. end
  303. end
  304. def test_should_raise_error_with_message_indicating_which_method_was_expected_to_be_called_on_which_mock_object_with_which_parameters_and_in_what_sequences
  305. mock = FakeMock.new('mock')
  306. sequence_one = Sequence.new('one')
  307. sequence_two = Sequence.new('two')
  308. expectation = Expectation.new(mock, :expected_method).with(1, 2, {'a' => true}, {:b => false}, [1, 2, 3]).in_sequence(sequence_one, sequence_two)
  309. assert !expectation.verified?
  310. assert_match "mock.expected_method(1, 2, {'a' => true}, {:b => false}, [1, 2, 3]); in sequence 'one'; in sequence 'two'", expectation.mocha_inspect
  311. end
  312. class FakeConstraint
  313. def initialize(allows_invocation_now)
  314. @allows_invocation_now = allows_invocation_now
  315. end
  316. def allows_invocation_now?
  317. @allows_invocation_now
  318. end
  319. end
  320. def test_should_be_in_correct_order_if_all_ordering_constraints_allow_invocation_now
  321. constraint_one = FakeConstraint.new(allows_invocation_now = true)
  322. constraint_two = FakeConstraint.new(allows_invocation_now = true)
  323. expectation = Expectation.new(nil, :method_one)
  324. expectation.add_ordering_constraint(constraint_one)
  325. expectation.add_ordering_constraint(constraint_two)
  326. assert expectation.in_correct_order?
  327. end
  328. def test_should_not_be_in_correct_order_if_one_ordering_constraint_does_not_allow_invocation_now
  329. constraint_one = FakeConstraint.new(allows_invocation_now = true)
  330. constraint_two = FakeConstraint.new(allows_invocation_now = false)
  331. expectation = Expectation.new(nil, :method_one)
  332. expectation.add_ordering_constraint(constraint_one)
  333. expectation.add_ordering_constraint(constraint_two)
  334. assert !expectation.in_correct_order?
  335. end
  336. def test_should_match_if_all_ordering_constraints_allow_invocation_now
  337. constraint_one = FakeConstraint.new(allows_invocation_now = true)
  338. constraint_two = FakeConstraint.new(allows_invocation_now = true)
  339. expectation = Expectation.new(nil, :method_one)
  340. expectation.add_ordering_constraint(constraint_one)
  341. expectation.add_ordering_constraint(constraint_two)
  342. assert expectation.match?(:method_one)
  343. end
  344. def test_should_not_match_if_one_ordering_constraints_does_not_allow_invocation_now
  345. constraint_one = FakeConstraint.new(allows_invocation_now = true)
  346. constraint_two = FakeConstraint.new(allows_invocation_now = false)
  347. expectation = Expectation.new(nil, :method_one)
  348. expectation.add_ordering_constraint(constraint_one)
  349. expectation.add_ordering_constraint(constraint_two)
  350. assert !expectation.match?(:method_one)
  351. end
  352. def test_should_not_be_satisfied_when_required_invocation_has_not_been_made
  353. expectation = Expectation.new(nil, :method_one).times(1)
  354. assert !expectation.satisfied?
  355. end
  356. def test_should_be_satisfied_when_required_invocation_has_been_made
  357. expectation = Expectation.new(nil, :method_one).times(1)
  358. expectation.invoke
  359. assert expectation.satisfied?
  360. end
  361. def test_should_not_be_satisfied_when_minimum_number_of_invocations_has_not_been_made
  362. expectation = Expectation.new(nil, :method_one).at_least(2)
  363. expectation.invoke
  364. assert !expectation.satisfied?
  365. end
  366. def test_should_be_satisfied_when_minimum_number_of_invocations_has_been_made
  367. expectation = Expectation.new(nil, :method_one).at_least(2)
  368. 2.times { expectation.invoke }
  369. assert expectation.satisfied?
  370. end
  371. class FakeSequence
  372. attr_reader :expectations
  373. def initialize
  374. @expectations = []
  375. end
  376. def constrain_as_next_in_sequence(expectation)
  377. @expectations << expectation
  378. end
  379. end
  380. def test_should_tell_sequences_to_constrain_expectation_as_next_in_sequence
  381. sequence_one = FakeSequence.new
  382. sequence_two = FakeSequence.new
  383. expectation = Expectation.new(nil, :method_one)
  384. assert_equal expectation, expectation.in_sequence(sequence_one, sequence_two)
  385. assert_equal [expectation], sequence_one.expectations
  386. assert_equal [expectation], sequence_two.expectations
  387. end
  388. class FakeState
  389. def initialize
  390. @active = false
  391. end
  392. def activate
  393. @active = true
  394. end
  395. def active?
  396. @active
  397. end
  398. end
  399. def test_should_change_state_when_expectation_is_invoked
  400. state = FakeState.new
  401. expectation = Expectation.new(nil, :method_one)
  402. expectation.then(state)
  403. expectation.invoke
  404. assert state.active?
  405. end
  406. def test_should_match_when_state_is_active
  407. state = FakeState.new
  408. expectation = Expectation.new(nil, :method_one)
  409. expectation.when(state)
  410. assert !expectation.match?(:method_one)
  411. state.activate
  412. assert expectation.match?(:method_one)
  413. end
  414. end