flexmock 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,5 +1,10 @@
1
1
  = Changes for FlexMock
2
2
 
3
+ == Version 0.3.2
4
+
5
+ * Fixed bug when mock is used as a replacement class in class
6
+ interception.
7
+
3
8
  == Version 0.3.1
4
9
 
5
10
  * Fixed some warnings regarding uniniitalized variables.
data/README CHANGED
@@ -3,7 +3,7 @@
3
3
  FlexMock is a simple mock object for unit testing. The interface is
4
4
  simple, but still provides a good bit of flexibility.
5
5
 
6
- Version :: 0.3.1
6
+ Version :: 0.3.2
7
7
 
8
8
  = Links
9
9
 
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ require 'rake/testtask'
8
8
 
9
9
  CLOBBER.include("html", 'pkg')
10
10
 
11
- PKG_VERSION = '0.3.1'
11
+ PKG_VERSION = '0.3.2'
12
12
 
13
13
  PKG_FILES = FileList[
14
14
  '[A-Z]*',
data/lib/flexmock.rb CHANGED
@@ -49,6 +49,8 @@ require 'test/unit'
49
49
  class FlexMock
50
50
  include Test::Unit::Assertions
51
51
 
52
+ class BadInterceptionError < RuntimeError; end
53
+
52
54
  attr_reader :mock_name, :mock_groups
53
55
  attr_accessor :mock_current_order
54
56
 
@@ -774,6 +776,7 @@ class FlexMock
774
776
  @intercepted = nil
775
777
  @target = nil
776
778
  @replacement = nil
779
+ @proxy = nil
777
780
  intercept(intercepted_class)
778
781
  update
779
782
  end
@@ -823,15 +826,15 @@ class FlexMock
823
826
 
824
827
  # Implement interception on the classes defined.
825
828
  def do_interception
826
- @target_class = coerce_class(@target)
827
- @replacement_class = coerce_class(@replacement)
829
+ @target_class = coerce_class(@target, "target")
830
+ @replacement_class = coerce_class(@replacement, "replacement")
828
831
  case @intercepted
829
832
  when String, Symbol
830
833
  @intercepted_name = @intercepted.to_s
831
834
  when Class
832
835
  @intercepted_name = @intercepted.name
833
836
  end
834
- @intercepted_class = coerce_class(@intercepted)
837
+ @intercepted_class = coerce_class(@intercepted, "intercepted")
835
838
  current_class = @target_class.const_get(@intercepted_name)
836
839
  if ClassProxy === current_class
837
840
  @proxy = current_class
@@ -845,14 +848,22 @@ class FlexMock
845
848
  end
846
849
 
847
850
  # Coerce a class object, string to symbol to be the class object.
848
- def coerce_class(klass)
851
+ def coerce_class(klass, where)
849
852
  case klass
850
853
  when String, Symbol
851
- Object.const_get(klass.to_s)
852
- when Class
854
+ lookup_const(klass.to_s, where)
855
+ else
853
856
  klass
854
857
  end
855
858
  end
859
+
860
+ def lookup_const(name, where, target=Object)
861
+ begin
862
+ target.const_get(name)
863
+ rescue NameError
864
+ raise BadInterceptionError, "in #{where} class #{name}"
865
+ end
866
+ end
856
867
  end
857
868
 
858
869
  ####################################################################
@@ -3,14 +3,6 @@
3
3
  require 'test/unit'
4
4
  require 'flexmock'
5
5
 
6
- module A
7
- class B
8
- def sample
9
- :ab
10
- end
11
- end
12
- end
13
-
14
6
  class Bark
15
7
  def listen
16
8
  :woof
@@ -30,9 +22,6 @@ class Dog
30
22
  def bark
31
23
  Bark.new
32
24
  end
33
- def wag
34
- A::B.new
35
- end
36
25
  def bark_id
37
26
  Bark.identify
38
27
  end
@@ -44,7 +33,6 @@ class TestClassInterception < Test::Unit::TestCase
44
33
  def test_original_functionality
45
34
  dog = Dog.new
46
35
  assert_equal :woof, dog.bark.listen
47
- assert_equal :ab, dog.wag.sample
48
36
  end
49
37
 
50
38
  def test_interception
@@ -97,6 +85,41 @@ class TestClassInterception < Test::Unit::TestCase
97
85
  assert_equal :barking, d.bark_id
98
86
  end
99
87
 
88
+ def test_interceptions_fails_with_bad_class_names
89
+ ex = assert_raises(FlexMock::BadInterceptionError) {
90
+ intercept("BADNAME").in("Dog").with("Meow")
91
+ }
92
+ assert_match(/intercepted/, ex.message)
93
+ assert_match(/BADNAME/, ex.message)
94
+
95
+ ex = assert_raises(FlexMock::BadInterceptionError) {
96
+ intercept(Bark).in("BADNAME").with("Meow")
97
+ }
98
+ assert_match(/target/, ex.message)
99
+ assert_match(/BADNAME/, ex.message)
100
+
101
+ ex = assert_raises(FlexMock::BadInterceptionError) {
102
+ intercept(Bark).in("Dog").with("BADNAME")
103
+ }
104
+ assert_match(/replacement/, ex.message)
105
+ assert_match(/BADNAME/, ex.message)
106
+ end
107
+
108
+ end
109
+
110
+ class TestClassInterceptionExample < Test::Unit::TestCase
111
+ include FlexMock::TestCase
112
+
113
+ def setup
114
+ @dog = Dog.new
115
+ @mock = flexmock('bark')
116
+ intercept(Bark).in(Dog).with(@mock)
117
+ end
118
+
119
+ def test_interception
120
+ @mock.should_receive(:new).with_no_args.once.and_return(:purr)
121
+ assert_equal :purr, @dog.bark
122
+ end
100
123
  end
101
124
 
102
125
  class TestMockFactory < Test::Unit::TestCase
metadata CHANGED
@@ -1,9 +1,9 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.11
2
+ rubygems_version: 0.8.99.1
3
3
  specification_version: 1
4
4
  name: flexmock
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.3.1
6
+ version: 0.3.2
7
7
  date: 2006-06-17 00:00:00 -04:00
8
8
  summary: Simple and Flexible Mock Objects for Testing
9
9
  require_paths:
@@ -25,6 +25,7 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
25
25
  platform: ruby
26
26
  signing_key:
27
27
  cert_chain:
28
+ post_install_message:
28
29
  authors:
29
30
  - Jim Weirich
30
31
  files: