flexmock 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -29,7 +29,7 @@ describe "FlexMock in a RSpec example" do
29
29
  m.should_receive(:hi).with().once
30
30
  lambda {
31
31
  flexmock_verify
32
- }.should raise_error(RSpec::Expectations::ExpectationNotMetError, /method 'hi\(\)' called incorrect number of times/)
32
+ }.should raise_error(RSpec::Expectations::ExpectationNotMetError, /\bhi\b.*incorrect.*times/i)
33
33
  end
34
34
 
35
35
  specify "should be able to create a stub" do
@@ -35,8 +35,8 @@ class TestFlexMockShoulds < Test::Unit::TestCase
35
35
  # Expected error messages on failures
36
36
  COUNT_ERROR_MESSAGE = /\bcalled\s+incorrect\s+number\s+of\s+times\b/
37
37
  NO_MATCH_ERROR_MESSAGE = /\bno\s+matching\s+handler\b/
38
- AT_LEAST_ERROR_MESSAGE = /\bshould\s+be\s+called\s+at\s+least\b/
39
- AT_MOST_ERROR_MESSAGE = /\bshould\s+be\s+called\s+at\s+most\b/
38
+ AT_LEAST_ERROR_MESSAGE = COUNT_ERROR_MESSAGE
39
+ AT_MOST_ERROR_MESSAGE = COUNT_ERROR_MESSAGE
40
40
  OUT_OF_ORDER_ERROR_MESSAGE = /\bcalled\s+out\s+of\s+order\b/
41
41
  NON_CONTAINER_MESSAGE = /\bis\s+not\s+in\s+a\s+container\b/
42
42
 
@@ -339,7 +339,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
339
339
  end
340
340
 
341
341
  def test_with_no_args_but_with_args
342
- assert_failure(NO_MATCH_ERROR_MESSAGE) do
342
+ assert_mock_failure(:message =>NO_MATCH_ERROR_MESSAGE, :deep => true) do
343
343
  FlexMock.use do |m|
344
344
  m.should_receive(:hi).with_no_args
345
345
  m.hi(1)
@@ -392,7 +392,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
392
392
  def test_with_ducktype_arg_matching_no_match
393
393
  FlexMock.use('greeter') do |m|
394
394
  m.should_receive(:hi).with(FlexMock.ducktype(:purr, :meow, :growl))
395
- assert_failure {
395
+ assert_mock_failure(:deep => true, :line => __LINE__+1) {
396
396
  m.hi(Cat.new)
397
397
  }
398
398
  end
@@ -408,7 +408,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
408
408
  def test_with_hash_non_matching
409
409
  FlexMock.use('greeter') do |m|
410
410
  m.should_receive(:hi).with(FlexMock.hsh(:a => 1, :b => 2))
411
- assert_failure {
411
+ assert_mock_failure(:deep => true, :line => __LINE__+1) {
412
412
  m.hi(:a => 1, :b => 4, :c => 3)
413
413
  }
414
414
  end
@@ -493,8 +493,8 @@ class TestFlexMockShoulds < Test::Unit::TestCase
493
493
  def test_arg_matching_with_no_match
494
494
  FlexMock.use do |m|
495
495
  m.should_receive(:hi).with(1).returns(10)
496
- assert_failure(NO_MATCH_ERROR_MESSAGE) {
497
- assert_equal 20, m.hi(2)
496
+ assert_mock_failure(:message =>NO_MATCH_ERROR_MESSAGE, :deep => true, :line => __LINE__+1) {
497
+ m.hi(2)
498
498
  }
499
499
  end
500
500
  end
@@ -502,7 +502,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
502
502
  def test_arg_matching_with_string_doesnt_over_match
503
503
  FlexMock.use do |m|
504
504
  m.should_receive(:hi).with(String).returns(20)
505
- assert_failure(NO_MATCH_ERROR_MESSAGE) {
505
+ assert_mock_failure(:message =>NO_MATCH_ERROR_MESSAGE, :deep => true, :line => __LINE__+1) {
506
506
  m.hi(1.0)
507
507
  }
508
508
  end
@@ -511,7 +511,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
511
511
  def test_block_arg_given_to_no_args
512
512
  FlexMock.use do |m|
513
513
  m.should_receive(:hi).with_no_args.returns(20)
514
- assert_failure(NO_MATCH_ERROR_MESSAGE) {
514
+ assert_mock_failure(:message =>NO_MATCH_ERROR_MESSAGE, :deep => true, :line => __LINE__+1) {
515
515
  m.hi { 1 }
516
516
  }
517
517
  end
@@ -551,7 +551,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
551
551
  end
552
552
 
553
553
  def test_never_and_called_once
554
- assert_failure(COUNT_ERROR_MESSAGE) do
554
+ assert_mock_failure(:message =>COUNT_ERROR_MESSAGE, :deep => true, :line => __LINE__+2) do
555
555
  FlexMock.use do |m|
556
556
  m.should_receive(:hi).with(1).never
557
557
  m.hi(1)
@@ -567,7 +567,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
567
567
  end
568
568
 
569
569
  def test_once_but_never_called
570
- assert_failure(COUNT_ERROR_MESSAGE) do
570
+ assert_mock_failure(:message =>COUNT_ERROR_MESSAGE, :line => __LINE__+2) do
571
571
  FlexMock.use do |m|
572
572
  m.should_receive(:hi).with(1).returns(10).once
573
573
  end
@@ -575,7 +575,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
575
575
  end
576
576
 
577
577
  def test_once_but_called_twice
578
- assert_failure(COUNT_ERROR_MESSAGE) do
578
+ assert_mock_failure(:message =>COUNT_ERROR_MESSAGE, :line => __LINE__+2) do
579
579
  FlexMock.use do |m|
580
580
  m.should_receive(:hi).with(1).returns(10).once
581
581
  m.hi(1)
@@ -627,7 +627,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
627
627
  end
628
628
 
629
629
  def test_at_least_but_never_called
630
- assert_failure(AT_LEAST_ERROR_MESSAGE) do
630
+ assert_mock_failure(:message =>AT_LEAST_ERROR_MESSAGE, :line => __LINE__+2) do
631
631
  FlexMock.use do |m|
632
632
  m.should_receive(:hi).with(1).returns(10).at_least.once
633
633
  end
@@ -643,7 +643,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
643
643
  end
644
644
 
645
645
  def test_at_least_and_exact
646
- assert_failure(COUNT_ERROR_MESSAGE) do
646
+ assert_mock_failure(:message =>COUNT_ERROR_MESSAGE, :line => __LINE__+2) do
647
647
  FlexMock.use do |m|
648
648
  m.should_receive(:hi).with(1).returns(10).at_least.once.once
649
649
  m.hi(1)
@@ -666,21 +666,23 @@ class TestFlexMockShoulds < Test::Unit::TestCase
666
666
  end
667
667
 
668
668
  def test_at_most_called_twice
669
- assert_failure(AT_MOST_ERROR_MESSAGE) do
669
+ ex = assert_mock_failure(:message =>AT_MOST_ERROR_MESSAGE, :line => __LINE__+2) do
670
670
  FlexMock.use do |m|
671
671
  m.should_receive(:hi).with(1).returns(10).at_most.once
672
672
  m.hi(1)
673
673
  m.hi(1)
674
674
  end
675
675
  end
676
+ assert_match(/at most 1/i, ex.message)
676
677
  end
677
678
 
678
679
  def test_at_most_and_at_least_called_never
679
- assert_failure(AT_LEAST_ERROR_MESSAGE) do
680
+ ex = assert_mock_failure(:message =>AT_LEAST_ERROR_MESSAGE, :line => __LINE__+2) do
680
681
  FlexMock.use do |m|
681
682
  m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
682
683
  end
683
684
  end
685
+ assert_match(/at least 1/i, ex.message)
684
686
  end
685
687
 
686
688
  def test_at_most_and_at_least_called_once
@@ -699,7 +701,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
699
701
  end
700
702
 
701
703
  def test_at_most_and_at_least_called_three_times
702
- assert_failure(AT_MOST_ERROR_MESSAGE) do
704
+ assert_mock_failure(:message =>AT_MOST_ERROR_MESSAGE, :line => __LINE__+2) do
703
705
  FlexMock.use do |m|
704
706
  m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
705
707
  m.hi(1)
@@ -722,7 +724,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
722
724
  end
723
725
 
724
726
  def test_call_counts_only_apply_to_matching_args_with_mismatch
725
- ex = assert_failure(COUNT_ERROR_MESSAGE) do
727
+ ex = assert_mock_failure(:message =>COUNT_ERROR_MESSAGE, :line => __LINE__+3) do
726
728
  FlexMock.use do |m|
727
729
  m.should_receive(:hi).with(1).once
728
730
  m.should_receive(:hi).with(2).twice
@@ -748,7 +750,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
748
750
  end
749
751
 
750
752
  def test_ordered_calls_out_of_order_will_fail
751
- assert_failure(OUT_OF_ORDER_ERROR_MESSAGE) do
753
+ assert_mock_failure(:message =>OUT_OF_ORDER_ERROR_MESSAGE, :deep => true, :line => __LINE__+6) do
752
754
  FlexMock.use 'm' do |m|
753
755
  m.should_receive(:hi).ordered
754
756
  m.should_receive(:lo).ordered
@@ -770,7 +772,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
770
772
  end
771
773
 
772
774
  def test_order_calls_with_different_arg_lists_and_out_of_order_will_fail
773
- assert_failure(OUT_OF_ORDER_ERROR_MESSAGE) do
775
+ assert_mock_failure(:message =>OUT_OF_ORDER_ERROR_MESSAGE, :deep => true, :line => __LINE__+6) do
774
776
  FlexMock.use 'm' do |m|
775
777
  m.should_receive(:hi).with("one").ordered
776
778
  m.should_receive(:hi).with("two").ordered
@@ -848,7 +850,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
848
850
  end
849
851
 
850
852
  def test_explicit_ordering_with_explicit_misorders
851
- assert_failure(OUT_OF_ORDER_ERROR_MESSAGE) do
853
+ assert_mock_failure(:message =>OUT_OF_ORDER_ERROR_MESSAGE, :deep => true, :line => __LINE__+6) do
852
854
  FlexMock.use 'm' do |m|
853
855
  m.should_receive(:hi).ordered(:first_group)
854
856
  m.should_receive(:lo).ordered(:second_group)
@@ -899,7 +901,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
899
901
  end
900
902
 
901
903
  def test_ordering_between_mocks_is_honored_for_global_ordering
902
- assert_failure(OUT_OF_ORDER_ERROR_MESSAGE) do
904
+ assert_mock_failure(:message =>OUT_OF_ORDER_ERROR_MESSAGE, :deep => true, :line => __LINE__+6) do
903
905
  FlexMock.use("x", "y") do |x, y|
904
906
  x.should_receive(:one).globally.ordered
905
907
  y.should_receive(:two).globally.ordered
@@ -1078,6 +1080,20 @@ class TestFlexMockShoulds < Test::Unit::TestCase
1078
1080
  assert ! mock.respond_to?(:phoo, true)
1079
1081
  end
1080
1082
 
1083
+ def test_backtraces_point_to_should_receive_line
1084
+ mock = flexmock("a mock")
1085
+ file_name_re = Regexp.quote(__FILE__)
1086
+ line_no = __LINE__ + 1
1087
+ mock.should_receive(:foo).and_return(:bar).once
1088
+ begin
1089
+ flexmock_verify
1090
+ rescue Exception => ex
1091
+ exception = ex
1092
+ end
1093
+ assert_not_nil exception
1094
+ assert_match(/#{file_name_re}:#{line_no}/, exception.backtrace.first)
1095
+ end
1096
+
1081
1097
  def test_can_mock_operators
1082
1098
  assert_operator(:[]) { |m| m[1] }
1083
1099
  assert_operator(:[]=) { |m| m[1] = :value }
data/test/test_setup.rb CHANGED
@@ -13,7 +13,9 @@ class FlexMock
13
13
  # Assertion helper used to assert validation failure. If a
14
14
  # message is given, then the error message should match the
15
15
  # expected error message.
16
- def assert_failure(message=nil)
16
+ def assert_failure(options={}, &block)
17
+ message = options[:message]
18
+ no_location = options[:no_location]
17
19
  ex = assert_raises(assertion_failed_error) { yield }
18
20
  if message
19
21
  case message
@@ -25,5 +27,43 @@ class FlexMock
25
27
  end
26
28
  ex
27
29
  end
30
+
31
+ # Similar to assert_failure, but assumes that a mock generated
32
+ # error object is return, so additional tests on the backtrace are
33
+ # added.
34
+ def assert_mock_failure(options={}, &block)
35
+ ex = assert_failure(options, &block)
36
+ file = eval("__FILE__", block.binding)
37
+ assert_matching_line(ex, file, options)
38
+ end
39
+
40
+ # Assert that there is a line matching file in the backtrace.
41
+ # Options are:
42
+ #
43
+ # deep: true -- matching line can be anywhere in backtrace,
44
+ # otherwise it must be the first.
45
+ #
46
+ # line: n -- Add a line number to the match
47
+ #
48
+ def assert_matching_line(ex, file, options)
49
+ line = options[:line]
50
+ search_all = options[:deep]
51
+ if line
52
+ loc_re = /#{Regexp.quote(file)}:#{line}/
53
+ else
54
+ loc_re = Regexp.compile(Regexp.quote(file))
55
+ end
56
+
57
+ if search_all
58
+ bts = ex.backtrace.join("\n")
59
+ assert_block("expected a backtrace line to match #{loc_re}\nBACKTRACE:\n#{bts}") {
60
+ ex.backtrace.any? { |bt| loc_re =~ bt }
61
+ }
62
+ else
63
+ assert_match(loc_re, ex.backtrace.first)
64
+ end
65
+
66
+ ex
67
+ end
28
68
  end
29
69
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: flexmock
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.2
5
+ version: 1.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jim Weirich
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-09-08 00:00:00 Z
13
+ date: 2012-09-18 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: "\n FlexMock is a extremely simple mock object class compatible\n with the Test::Unit framework. Although the FlexMock's\n interface is simple, it is very flexible.\n "