1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00

Add testing the class of a serializer instance

This commit is contained in:
Håkon Lerring 2012-03-15 16:58:24 +01:00 committed by Gabe Berke-Williams
parent f1a3dca5b9
commit d9906d84ce
2 changed files with 46 additions and 14 deletions

View file

@ -24,6 +24,11 @@ module Shoulda # :nodoc:
@type = type
self
end
def as_instance_of(type)
@instance_type = type
self
end
def matches?(subject)
@subject = subject
@ -58,25 +63,27 @@ module Shoulda # :nodoc:
false
end
end
def type_valid?
def class_valid?
if @type
klass = model_class.serialized_attributes[@name]
if klass == @type
true
else
if klass.object_class == @type
true
else
@missing = ":#{@name} should be a type of #{@type}"
false
end
end
model_class.serialized_attributes[@name] == @type
else
true
end
end
def instance_class_valid?
if @instance_type
klass = model_class.serialized_attributes[@name].class
klass == @instance_type
else
true
end
end
def type_valid?
class_valid? && instance_class_valid?
end
def expectation
expectation = "#{model_class.name} to serialize the attribute called :#{@name}"

View file

@ -23,6 +23,10 @@ describe Shoulda::Matchers::ActiveRecord::SerializeMatcher do
it "should be serialized" do
model.should serialize(:attr).as(Hash)
end
it "should not match when using as_instance_of" do
model.should_not serialize(:attr).as_instance_of(Hash)
end
end
context "an attribute that should be serialized with a type of Array" do
@ -52,5 +56,26 @@ describe Shoulda::Matchers::ActiveRecord::SerializeMatcher do
matcher.matches?(model).should == false
matcher.failure_message.should_not be_nil
end
context "a serializer that is an instance of a class" do
before do
define_constant(:ExampleSerializer, Object) do
def load(*); end
def dump(*); end
end
define_model :example, :attr => :string do
serialize :attr, ExampleSerializer.new
end
@model = Example.new
end
it "should match when using as_instance_of" do
@model.should serialize(:attr).as_instance_of(ExampleSerializer)
end
it "should not match when using as" do
@model.should_not serialize(:attr).as(ExampleSerializer)
end
end
end
end