Skip to content
This repository
Browse code

Add testing the class of a serializer instance

  • Loading branch information...
commit d9906d84ce188474a1114cac8870b18d8a3b5287 1 parent f1a3dca
Håkon Lerring authored March 15, 2012 gabebw committed March 16, 2012
35  lib/shoulda/matchers/active_record/serialize_matcher.rb
@@ -24,6 +24,11 @@ def as(type)
24 24
           @type = type
25 25
           self
26 26
         end
  27
+        
  28
+        def as_instance_of(type)
  29
+          @instance_type = type
  30
+          self
  31
+        end
27 32
 
28 33
         def matches?(subject)
29 34
           @subject = subject
@@ -58,26 +63,28 @@ def serialization_valid?
58 63
             false
59 64
           end
60 65
         end
61  
-
62  
-        def type_valid?
  66
+        
  67
+        def class_valid?
63 68
           if @type
64  
-            klass = model_class.serialized_attributes[@name]
65  
-
66  
-            if klass == @type
67  
-              true
68  
-            else
69  
-              if klass.object_class == @type
70  
-                true
71  
-              else
72  
-                @missing = ":#{@name} should be a type of #{@type}"
73  
-                false
74  
-              end
75  
-            end
  69
+            model_class.serialized_attributes[@name] == @type
  70
+          else
  71
+            true
  72
+          end
  73
+        end
  74
+        
  75
+        def instance_class_valid?
  76
+          if @instance_type
  77
+            klass = model_class.serialized_attributes[@name].class
  78
+            klass == @instance_type
76 79
           else
77 80
             true
78 81
           end
79 82
         end
80 83
 
  84
+        def type_valid?
  85
+          class_valid? && instance_class_valid?
  86
+        end
  87
+
81 88
         def expectation
82 89
           expectation = "#{model_class.name} to serialize the attribute called :#{@name}"
83 90
           expectation += " with a type of #{@type}" if @type
25  spec/shoulda/active_record/serialize_matcher_spec.rb
@@ -23,6 +23,10 @@
23 23
     it "should be serialized" do
24 24
       model.should serialize(:attr).as(Hash)
25 25
     end
  26
+
  27
+    it "should not match when using as_instance_of" do
  28
+      model.should_not serialize(:attr).as_instance_of(Hash)
  29
+    end
26 30
   end
27 31
 
28 32
   context "an attribute that should be serialized with a type of Array" do
@@ -52,5 +56,26 @@
52 56
       matcher.matches?(model).should == false
53 57
       matcher.failure_message.should_not be_nil
54 58
     end
  59
+
  60
+    context "a serializer that is an instance of a class" do
  61
+      before do
  62
+        define_constant(:ExampleSerializer, Object) do
  63
+          def load(*); end
  64
+          def dump(*); end
  65
+        end
  66
+        define_model :example, :attr => :string do
  67
+          serialize :attr, ExampleSerializer.new
  68
+        end
  69
+        @model = Example.new
  70
+      end
  71
+
  72
+      it "should match when using as_instance_of" do
  73
+        @model.should serialize(:attr).as_instance_of(ExampleSerializer)
  74
+      end
  75
+
  76
+      it "should not match when using as" do
  77
+        @model.should_not serialize(:attr).as(ExampleSerializer)
  78
+      end
  79
+    end
55 80
   end
56 81
 end

0 notes on commit d9906d8

Please sign in to comment.
Something went wrong with that request. Please try again.