flexmock 0.7.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,12 +11,12 @@
11
11
 
12
12
  require 'test/unit'
13
13
  require 'flexmock'
14
+ require 'test/asserts'
14
15
 
15
16
  def mock_top_level_function
16
17
  :mtlf
17
18
  end
18
19
 
19
-
20
20
  module Kernel
21
21
  def mock_kernel_function
22
22
  :mkf
@@ -83,6 +83,16 @@ class TestFlexMockShoulds < Test::Unit::TestCase
83
83
  end
84
84
  end
85
85
 
86
+ def test_block_example_from_readme
87
+ FlexMock.use do |m|
88
+ m.should_receive(:foo).with(Integer,Proc).and_return(:got_block)
89
+ m.should_receive(:foo).with(Integer).and_return(:no_block)
90
+
91
+ assert_equal :no_block, m.foo(1)
92
+ assert_equal :got_block, m.foo(1) { }
93
+ end
94
+ end
95
+
86
96
  def test_return_with_and_without_block_interleaved
87
97
  FlexMock.use do |m|
88
98
  m.should_receive(:hi).and_return(:a).and_return { :b }.and_return(:c)
@@ -100,6 +110,16 @@ class TestFlexMockShoulds < Test::Unit::TestCase
100
110
  end
101
111
  end
102
112
 
113
+ def test_and_return_undefined
114
+ FlexMock.use do |m|
115
+ m.should_receive(:foo).and_return_undefined
116
+ m.should_receive(:phoo).returns_undefined
117
+ assert_equal FlexMock.undefined, m.foo
118
+ assert_equal FlexMock.undefined, m.foo.bar.baz.bing.ka_ching
119
+ assert_equal FlexMock.undefined, m.phoo.bar.baz.bing.ka_ching
120
+ end
121
+ end
122
+
103
123
  def test_and_yield_will_continue_to_yield_the_same_value
104
124
  FlexMock.use do |m|
105
125
  m.should_receive(:hi).and_yield(:yield_value)
@@ -865,6 +885,109 @@ class TestFlexMockShoulds < Test::Unit::TestCase
865
885
  assert_equal :mkf, m2.mock_kernel_function
866
886
  end
867
887
 
888
+ def test_expectations_can_by_marked_as_default
889
+ m = flexmock("m")
890
+ m.should_receive(:foo).and_return(:bar).by_default
891
+ assert_equal :bar, m.foo
892
+ end
893
+
894
+ def test_default_expectations_are_search_in_the_proper_order
895
+ m = flexmock("m")
896
+ m.should_receive(:foo).with(Integer).once.and_return(:first).by_default
897
+ m.should_receive(:foo).with(1).and_return(:second).by_default
898
+ assert_equal :first, m.foo(1)
899
+ assert_equal :second, m.foo(1)
900
+ end
901
+
902
+ def test_expectations_with_count_constraints_can_by_marked_as_default
903
+ m = flexmock("m")
904
+ m.should_receive(:foo).and_return(:bar).once.by_default
905
+ assert_raise Test::Unit::AssertionFailedError do
906
+ flexmock_teardown
907
+ end
908
+ end
909
+
910
+ def test_default_expectations_are_overridden_by_later_expectations
911
+ m = flexmock("m")
912
+ m.should_receive(:foo).and_return(:bar).once.by_default
913
+ m.should_receive(:foo).and_return(:bar).twice
914
+ m.foo
915
+ m.foo
916
+ end
917
+
918
+ def test_ordered_default_expectations_can_be_specified
919
+ m = flexmock("m")
920
+ m.should_receive(:foo).ordered.by_default
921
+ m.should_receive(:bar).ordered.by_default
922
+ m.bar
923
+ assert_raise Test::Unit::AssertionFailedError do m.foo end
924
+ end
925
+
926
+ def test_ordered_default_expectations_can_be_overridden
927
+ m = flexmock("m")
928
+ m.should_receive(:foo).ordered.by_default
929
+ m.should_receive(:bar).ordered.by_default
930
+
931
+ m.should_receive(:bar).ordered
932
+ m.should_receive(:foo).ordered
933
+
934
+ m.bar
935
+ m.foo
936
+ end
937
+
938
+ def test_by_default_works_at_mock_level
939
+ m = flexmock("m",
940
+ :foo => :bar,
941
+ :pooh => :bear,
942
+ :who => :dey).by_default
943
+ m.should_receive(:pooh => :winnie)
944
+ assert_equal :bar, m.foo
945
+ assert_equal :dey, m.who
946
+ assert_equal :winnie, m.pooh
947
+ end
948
+
949
+ def test_by_default_at_mock_level_does_nothing_with_no_expectations
950
+ assert_nothing_raised do
951
+ flexmock("m").by_default
952
+ end
953
+ end
954
+
955
+ def test_partial_mocks_can_have_default_expectations
956
+ obj = Object.new
957
+ flexmock(obj).should_receive(:foo).and_return(:bar).by_default
958
+ assert_equal :bar, obj.foo
959
+ end
960
+
961
+ def test_partial_mocks_can_have_default_expectations_overridden
962
+ obj = Object.new
963
+ flexmock(obj).should_receive(:foo).and_return(:bar).by_default
964
+ flexmock(obj).should_receive(:foo).and_return(:baz)
965
+ assert_equal :baz, obj.foo
966
+ end
967
+
968
+ def test_wicked_and_evil_tricks_with_by_default_are_thwarted
969
+ mock = flexmock("mock")
970
+ exp = mock.should_receive(:foo).and_return(:first).once
971
+ mock.should_receive(:foo).and_return(:second)
972
+ ex = assert_raise(FlexMock::UsageError) do
973
+ exp.by_default
974
+ end
975
+ assert_match %r(previously defined), ex.message
976
+ assert_equal :first, mock.foo
977
+ assert_equal :second, mock.foo
978
+ end
979
+
980
+ def test_mocks_can_handle_multi_parameter_respond_tos
981
+ mock = flexmock("a mock", :foo => :bar)
982
+ assert mock.respond_to?(:foo)
983
+ assert mock.respond_to?(:foo, true)
984
+ assert mock.respond_to?(:foo, false)
985
+
986
+ assert ! mock.respond_to?(:phoo)
987
+ assert ! mock.respond_to?(:phoo, false)
988
+ assert ! mock.respond_to?(:phoo, true)
989
+ end
990
+
868
991
  def test_can_mock_operators
869
992
  assert_operator(:[]) { |m| m[1] }
870
993
  assert_operator(:[]=) { |m| m[1] = :value }
@@ -876,7 +999,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
876
999
  assert_operator(:*) { |m| m * :x }
877
1000
  assert_operator(:"/") { |m| m / :x }
878
1001
  assert_operator(:%) { |m| m % :x }
879
- assert_operator(:~) { |m| ~m }
1002
+ assert_operator(:~) { |m| ~m } # )
880
1003
  assert_operator(:&) { |m| m & :x }
881
1004
  assert_operator(:|) { |m| m | :x }
882
1005
  assert_operator(:^) { |m| m ^ :x }
@@ -891,7 +1014,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
891
1014
  assert_operator(:<=>) { |m| m <=> :x }
892
1015
  assert_operator(:=~) { |m| m =~ :x }
893
1016
  end
894
-
1017
+
895
1018
  private
896
1019
 
897
1020
  def assert_operator(op, &block)
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #---
4
+ # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
5
+ # All rights reserved.
6
+
7
+ # Permission is granted for use, copying, modification, distribution,
8
+ # and distribution of modified versions of this work as long as the
9
+ # above copyright notice is included.
10
+ #+++
11
+
12
+ require "test/unit"
13
+ require "flexmock"
14
+
15
+ class UndefinedTest < Test::Unit::TestCase
16
+ def test_undefined_method_calls_return_undefined
17
+ assert_undefined undefined.some_random_undefined_method
18
+ end
19
+
20
+ def test_equals
21
+ assert undefined == undefined
22
+ assert ! (undefined == Object.new)
23
+ end
24
+
25
+ def test_math_operators
26
+ assert_undefined undefined + 1
27
+ assert_undefined undefined - 1
28
+ assert_undefined undefined * 1
29
+ assert_undefined undefined / 1
30
+ assert_undefined undefined ** 1
31
+ end
32
+
33
+ def test_math_operators_reversed
34
+ assert_undefined 1 + undefined
35
+ assert_undefined 1 - undefined
36
+ assert_undefined 1 * undefined
37
+ assert_undefined 1 / undefined
38
+ assert_undefined 2 ** undefined
39
+ end
40
+
41
+ def test_comparisons
42
+ assert_undefined undefined < 1
43
+ assert_undefined undefined <= 1
44
+ assert_undefined undefined > 1
45
+ assert_undefined undefined >= 1
46
+ assert_undefined undefined <=> 1
47
+ end
48
+
49
+ def test_comparisons_reversed
50
+ assert_undefined 1 < undefined
51
+ assert_undefined 1 <= undefined
52
+ assert_undefined 1 > undefined
53
+ assert_undefined 1 >= undefined
54
+ assert_undefined 1 <=> undefined
55
+ end
56
+
57
+ def test_base_level_methods
58
+ assert_kind_of FlexMock::Undefined, undefined
59
+ end
60
+
61
+ def test_cant_create_a_new_undefined
62
+ assert_raises(NoMethodError) do FlexMock::Undefined.new end
63
+ end
64
+
65
+ def test_cant_clone_undefined
66
+ assert_undefined undefined.clone
67
+ assert_equal undefined.__id__, undefined.clone.__id__
68
+ end
69
+
70
+ def test_string_representations
71
+ assert_equal "-UNDEFINED-", undefined.to_s
72
+ assert_equal "-UNDEFINED-", undefined.inspect
73
+ end
74
+
75
+ def test_undefined_is_not_nil
76
+ assert ! undefined.nil?
77
+ end
78
+
79
+ private
80
+
81
+ def assert_undefined(obj)
82
+ assert undefined == obj
83
+ end
84
+
85
+ def undefined
86
+ FlexMock.undefined
87
+ end
88
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: flexmock
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.7.1
7
- date: 2007-09-24 00:00:00 -04:00
6
+ version: 0.8.0
7
+ date: 2007-11-08 00:00:00 -05:00
8
8
  summary: Simple and Flexible Mock Objects for Testing
9
9
  require_paths:
10
10
  - lib
@@ -42,33 +42,41 @@ files:
42
42
  - lib/flexmock/core_class_methods.rb
43
43
  - lib/flexmock/default_framework_adapter.rb
44
44
  - lib/flexmock/deprecated_methods.rb
45
+ - lib/flexmock/errors.rb
45
46
  - lib/flexmock/expectation.rb
46
47
  - lib/flexmock/expectation_director.rb
47
48
  - lib/flexmock/mock_container.rb
48
49
  - lib/flexmock/noop.rb
49
50
  - lib/flexmock/ordering.rb
50
51
  - lib/flexmock/partial_mock.rb
52
+ - lib/flexmock/rails.rb
51
53
  - lib/flexmock/recorder.rb
52
54
  - lib/flexmock/rspec.rb
53
55
  - lib/flexmock/test_unit.rb
54
56
  - lib/flexmock/test_unit_integration.rb
57
+ - lib/flexmock/undefined.rb
55
58
  - lib/flexmock/validators.rb
59
+ - lib/flexmock/rails/view_mocking.rb
56
60
  - test/asserts.rb
57
61
  - test/redirect_error.rb
62
+ - test/test_aliasing.rb
58
63
  - test/test_container_methods.rb
59
64
  - test/test_default_framework_adapter.rb
60
65
  - test/test_demeter_mocking.rb
61
66
  - test/test_deprecated_methods.rb
62
- - test/test_example.rb
67
+ - test/test_examples_from_readme.rb
63
68
  - test/test_extended_should_receive.rb
64
69
  - test/test_flexmodel.rb
65
70
  - test/test_naming.rb
66
71
  - test/test_new_instances.rb
67
72
  - test/test_partial_mock.rb
73
+ - test/test_rails_view_stub.rb
68
74
  - test/test_record_mode.rb
69
75
  - test/test_samples.rb
76
+ - test/test_should_ignore_missing.rb
70
77
  - test/test_should_receive.rb
71
78
  - test/test_tu_integration.rb
79
+ - test/test_undefined.rb
72
80
  - test/rspec_integration/integration_spec.rb
73
81
  - test/test_unit_integration/test_auto_test_unit.rb
74
82
  - flexmock.blurb
@@ -87,6 +95,7 @@ files:
87
95
  - doc/releases/flexmock-0.6.4.rdoc
88
96
  - doc/releases/flexmock-0.7.0.rdoc
89
97
  - doc/releases/flexmock-0.7.1.rdoc
98
+ - doc/releases/flexmock-0.8.0.rdoc
90
99
  test_files: []
91
100
 
92
101
  rdoc_options:
@@ -112,6 +121,7 @@ extra_rdoc_files:
112
121
  - doc/releases/flexmock-0.6.4.rdoc
113
122
  - doc/releases/flexmock-0.7.0.rdoc
114
123
  - doc/releases/flexmock-0.7.1.rdoc
124
+ - doc/releases/flexmock-0.8.0.rdoc
115
125
  executables: []
116
126
 
117
127
  extensions: []
data/test/test_example.rb DELETED
@@ -1,36 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- #---
4
- # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
5
- # All rights reserved.
6
-
7
- # Permission is granted for use, copying, modification, distribution,
8
- # and distribution of modified versions of this work as long as the
9
- # above copyright notice is included.
10
- #+++
11
-
12
- require 'test/unit'
13
- require 'flexmock'
14
-
15
- class TemperatureSampler
16
- def initialize(sensor)
17
- @sensor = sensor
18
- end
19
-
20
- def average_temp
21
- total = (0...3).collect { @sensor.read_temperature }.inject { |i, s| i + s }
22
- total / 3.0
23
- end
24
- end
25
-
26
- class TestTemperatureSampler < Test::Unit::TestCase
27
- include FlexMock::TestCase
28
-
29
- def test_tempurature_sampler
30
- readings = [10, 12, 14]
31
- mock_sensor = flexmock("sensor")
32
- mock_sensor.should_receive(:read_temperature).and_return { readings.shift }
33
- sampler = TemperatureSampler.new(mock_sensor)
34
- assert_equal 12, sampler.average_temp
35
- end
36
- end