PageRenderTime 29ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/jruby-1.7.3/build_lib/mocha/test/unit/expectation_test.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 480 lines | 392 code | 88 blank | 0 comment | 3 complexity | 4db87047e1e7a282d5dba03b213013fa 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_same_specified_value_multiple_times
  129. expectation = new_expectation.returns(99)
  130. assert_equal 99, expectation.invoke
  131. assert_equal 99, expectation.invoke
  132. end
  133. def test_should_return_specified_values_on_consecutive_calls
  134. expectation = new_expectation.returns(99, 100, 101)
  135. assert_equal 99, expectation.invoke
  136. assert_equal 100, expectation.invoke
  137. assert_equal 101, expectation.invoke
  138. end
  139. def test_should_return_specified_values_on_consecutive_calls_even_if_values_are_modified
  140. values = [99, 100, 101]
  141. expectation = new_expectation.returns(*values)
  142. values.shift
  143. assert_equal 99, expectation.invoke
  144. assert_equal 100, expectation.invoke
  145. assert_equal 101, expectation.invoke
  146. end
  147. def test_should_return_nil_by_default
  148. assert_nil new_expectation.invoke
  149. end
  150. def test_should_return_nil_if_no_value_specified
  151. expectation = new_expectation.returns()
  152. assert_nil expectation.invoke
  153. end
  154. def test_should_raise_runtime_exception
  155. expectation = new_expectation.raises
  156. assert_raise(RuntimeError) { expectation.invoke }
  157. end
  158. def test_should_raise_custom_exception
  159. exception = Class.new(Exception)
  160. expectation = new_expectation.raises(exception)
  161. assert_raise(exception) { expectation.invoke }
  162. end
  163. def test_should_raise_same_instance_of_custom_exception
  164. exception_klass = Class.new(StandardError)
  165. expected_exception = exception_klass.new
  166. expectation = new_expectation.raises(expected_exception)
  167. actual_exception = assert_raise(exception_klass) { expectation.invoke }
  168. assert_same expected_exception, actual_exception
  169. end
  170. def test_should_use_the_default_exception_message
  171. expectation = new_expectation.raises(Exception)
  172. exception = assert_raise(Exception) { expectation.invoke }
  173. assert_equal Exception.new.message, exception.message
  174. end
  175. def test_should_raise_custom_exception_with_message
  176. exception_msg = "exception message"
  177. expectation = new_expectation.raises(Exception, exception_msg)
  178. exception = assert_raise(Exception) { expectation.invoke }
  179. assert_equal exception_msg, exception.message
  180. end
  181. def test_should_return_values_then_raise_exception
  182. expectation = new_expectation.returns(1, 2).then.raises()
  183. assert_equal 1, expectation.invoke
  184. assert_equal 2, expectation.invoke
  185. assert_raise(RuntimeError) { expectation.invoke }
  186. end
  187. def test_should_raise_exception_then_return_values
  188. expectation = new_expectation.raises().then.returns(1, 2)
  189. assert_raise(RuntimeError) { expectation.invoke }
  190. assert_equal 1, expectation.invoke
  191. assert_equal 2, expectation.invoke
  192. end
  193. def test_should_verify_successfully_if_expected_call_was_made
  194. expectation = new_expectation
  195. expectation.invoke
  196. assert expectation.verified?
  197. end
  198. def test_should_not_verify_successfully_if_call_expected_once_but_invoked_twice
  199. expectation = new_expectation.once
  200. expectation.invoke
  201. expectation.invoke
  202. assert !expectation.verified?
  203. end
  204. def test_should_not_verify_successfully_if_call_expected_once_but_not_invoked
  205. expectation = new_expectation.once
  206. assert !expectation.verified?
  207. end
  208. def test_should_verify_successfully_if_call_expected_once_and_invoked_once
  209. expectation = new_expectation.once
  210. expectation.invoke
  211. assert expectation.verified?
  212. end
  213. def test_should_not_verify_successfully_if_call_expected_twice_and_invoked_three_times
  214. expectation = new_expectation.twice
  215. expectation.invoke
  216. expectation.invoke
  217. expectation.invoke
  218. assert !expectation.verified?
  219. end
  220. def test_should_not_verify_successfully_if_call_expected_twice_but_invoked_once
  221. expectation = new_expectation.twice
  222. expectation.invoke
  223. assert !expectation.verified?
  224. end
  225. def test_should_verify_successfully_if_call_expected_twice_and_invoked_twice
  226. expectation = new_expectation.twice
  227. expectation.invoke
  228. expectation.invoke
  229. assert expectation.verified?
  230. end
  231. def test_should_verify_successfully_if_expected_call_was_made_at_least_once
  232. expectation = new_expectation.at_least_once
  233. 3.times {expectation.invoke}
  234. assert expectation.verified?
  235. end
  236. def test_should_not_verify_successfully_if_expected_call_was_not_made_at_least_once
  237. expectation = new_expectation.with(1, 2, 3).at_least_once
  238. assert !expectation.verified?
  239. assert_match(/expected at least once, not yet invoked/i, expectation.mocha_inspect)
  240. end
  241. def test_should_verify_successfully_if_expected_call_was_made_expected_number_of_times
  242. expectation = new_expectation.times(2)
  243. 2.times {expectation.invoke}
  244. assert expectation.verified?
  245. end
  246. def test_should_not_verify_successfully_if_expected_call_was_made_too_few_times
  247. expectation = new_expectation.times(2)
  248. 1.times {expectation.invoke}
  249. assert !expectation.verified?
  250. assert_match(/expected exactly twice, already invoked once/i, expectation.mocha_inspect)
  251. end
  252. def test_should_not_verify_successfully_if_expected_call_was_made_too_many_times
  253. expectation = new_expectation.times(2)
  254. 3.times {expectation.invoke}
  255. assert !expectation.verified?
  256. end
  257. def test_should_increment_assertion_counter_for_expectation_because_it_does_need_verifyng
  258. expectation = new_expectation
  259. expectation.invoke
  260. assertion_counter = SimpleCounter.new
  261. expectation.verified?(assertion_counter)
  262. assert_equal 1, assertion_counter.count
  263. end
  264. def test_should_not_increment_assertion_counter_for_stub_because_it_does_not_need_verifying
  265. stub = Expectation.new(nil, :expected_method).at_least(0)
  266. assertion_counter = SimpleCounter.new
  267. stub.verified?(assertion_counter)
  268. assert_equal 0, assertion_counter.count
  269. end
  270. def test_should_store_backtrace_from_point_where_expectation_was_created
  271. execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
  272. assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
  273. end
  274. class FakeMock
  275. def initialize(name)
  276. @name = name
  277. end
  278. def mocha_inspect
  279. @name
  280. end
  281. end
  282. 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
  283. mock = FakeMock.new('mock')
  284. sequence_one = Sequence.new('one')
  285. sequence_two = Sequence.new('two')
  286. expectation = Expectation.new(mock, :expected_method).with(1, 2, {'a' => true}, {:b => false}, [1, 2, 3]).in_sequence(sequence_one, sequence_two)
  287. assert !expectation.verified?
  288. assert_match "mock.expected_method(1, 2, {'a' => true}, {:b => false}, [1, 2, 3]); in sequence 'one'; in sequence 'two'", expectation.mocha_inspect
  289. end
  290. class FakeConstraint
  291. def initialize(allows_invocation_now)
  292. @allows_invocation_now = allows_invocation_now
  293. end
  294. def allows_invocation_now?
  295. @allows_invocation_now
  296. end
  297. end
  298. def test_should_be_in_correct_order_if_all_ordering_constraints_allow_invocation_now
  299. constraint_one = FakeConstraint.new(allows_invocation_now = true)
  300. constraint_two = FakeConstraint.new(allows_invocation_now = true)
  301. expectation = Expectation.new(nil, :method_one)
  302. expectation.add_ordering_constraint(constraint_one)
  303. expectation.add_ordering_constraint(constraint_two)
  304. assert expectation.in_correct_order?
  305. end
  306. def test_should_not_be_in_correct_order_if_one_ordering_constraint_does_not_allow_invocation_now
  307. constraint_one = FakeConstraint.new(allows_invocation_now = true)
  308. constraint_two = FakeConstraint.new(allows_invocation_now = false)
  309. expectation = Expectation.new(nil, :method_one)
  310. expectation.add_ordering_constraint(constraint_one)
  311. expectation.add_ordering_constraint(constraint_two)
  312. assert !expectation.in_correct_order?
  313. end
  314. def test_should_match_if_all_ordering_constraints_allow_invocation_now
  315. constraint_one = FakeConstraint.new(allows_invocation_now = true)
  316. constraint_two = FakeConstraint.new(allows_invocation_now = true)
  317. expectation = Expectation.new(nil, :method_one)
  318. expectation.add_ordering_constraint(constraint_one)
  319. expectation.add_ordering_constraint(constraint_two)
  320. assert expectation.match?(:method_one)
  321. end
  322. def test_should_not_match_if_one_ordering_constraints_does_not_allow_invocation_now
  323. constraint_one = FakeConstraint.new(allows_invocation_now = true)
  324. constraint_two = FakeConstraint.new(allows_invocation_now = false)
  325. expectation = Expectation.new(nil, :method_one)
  326. expectation.add_ordering_constraint(constraint_one)
  327. expectation.add_ordering_constraint(constraint_two)
  328. assert !expectation.match?(:method_one)
  329. end
  330. def test_should_not_be_satisfied_when_required_invocation_has_not_been_made
  331. expectation = Expectation.new(nil, :method_one).times(1)
  332. assert !expectation.satisfied?
  333. end
  334. def test_should_be_satisfied_when_required_invocation_has_been_made
  335. expectation = Expectation.new(nil, :method_one).times(1)
  336. expectation.invoke
  337. assert expectation.satisfied?
  338. end
  339. def test_should_not_be_satisfied_when_minimum_number_of_invocations_has_not_been_made
  340. expectation = Expectation.new(nil, :method_one).at_least(2)
  341. expectation.invoke
  342. assert !expectation.satisfied?
  343. end
  344. def test_should_be_satisfied_when_minimum_number_of_invocations_has_been_made
  345. expectation = Expectation.new(nil, :method_one).at_least(2)
  346. 2.times { expectation.invoke }
  347. assert expectation.satisfied?
  348. end
  349. class FakeSequence
  350. attr_reader :expectations
  351. def initialize
  352. @expectations = []
  353. end
  354. def constrain_as_next_in_sequence(expectation)
  355. @expectations << expectation
  356. end
  357. end
  358. def test_should_tell_sequences_to_constrain_expectation_as_next_in_sequence
  359. sequence_one = FakeSequence.new
  360. sequence_two = FakeSequence.new
  361. expectation = Expectation.new(nil, :method_one)
  362. assert_equal expectation, expectation.in_sequence(sequence_one, sequence_two)
  363. assert_equal [expectation], sequence_one.expectations
  364. assert_equal [expectation], sequence_two.expectations
  365. end
  366. class FakeState
  367. def initialize
  368. @active = false
  369. end
  370. def activate
  371. @active = true
  372. end
  373. def active?
  374. @active
  375. end
  376. end
  377. def test_should_change_state_when_expectation_is_invoked
  378. state = FakeState.new
  379. expectation = Expectation.new(nil, :method_one)
  380. expectation.then(state)
  381. expectation.invoke
  382. assert state.active?
  383. end
  384. def test_should_match_when_state_is_active
  385. state = FakeState.new
  386. expectation = Expectation.new(nil, :method_one)
  387. expectation.when(state)
  388. assert !expectation.match?(:method_one)
  389. state.activate
  390. assert expectation.match?(:method_one)
  391. end
  392. end