Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'serialize-matcher' of https://github.com/carrot/shoulda…

  • Loading branch information...
commit bfe3f5ebdeb9452c88c974c5d0bc8d0b98d48e7f 2 parents d38edbd + a9a4796
@mike-burns mike-burns authored
View
2  lib/shoulda/matchers/active_record.rb
@@ -2,7 +2,7 @@
require 'shoulda/matchers/active_record/have_db_column_matcher'
require 'shoulda/matchers/active_record/have_db_index_matcher'
require 'shoulda/matchers/active_record/have_readonly_attribute_matcher'
-
+require 'shoulda/matchers/active_record/serialize_matcher'
module Shoulda
module Matchers
View
89 lib/shoulda/matchers/active_record/serialize_matcher.rb
@@ -0,0 +1,89 @@
+module Shoulda # :nodoc:
+ module Matchers
+ module ActiveRecord # :nodoc:
+
+ # Ensure that the field becomes serialized.
+ #
+ # Options:
+ # * <tt>:as</tt> - tests that the serialized attribute makes use of the class_name option.
+ #
+ # Example:
+ # it { should serialize(:details) }
+ # it { should serialize(:details).as(Hash) }
+ #
+ def serialize(name)
+ SerializeMatcher.new(name)
+ end
+
+ class SerializeMatcher # :nodoc:
+ def initialize(name)
+ @name = name.to_s
+ end
+
+ def as(type)
+ @type = type
+ self
+ end
+
+ def matches?(subject)
+ @subject = subject
+ serialization_valid? && type_valid?
+ end
+
+ def failure_message
+ "Expected #{expectation} (#{@missing})"
+ end
+
+ def negative_failure_message
+ "Did not expect #{expectation}"
+ end
+
+ def description
+ description = "serialize :#{@name}"
+ description += " class_name => #{@type}" if @type
+ description
+ end
+
+ protected
+
+ def model_class
+ @subject.class
+ end
+
+ def serialization_valid?
+ if model_class.serialized_attributes.keys.include?(@name)
+ true
+ else
+ @missing = "no serialized attribute called :#{@name}"
+ false
+ end
+ end
+
+ def type_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
+ else
+ true
+ end
+ end
+
+ def expectation
+ expectation = "#{model_class.name} to serialize the attribute called :#{@name}"
+ expectation += " with a type of #{@type}" if @type
+ expectation
+ end
+ end
+ end
+ end
+end
View
62 spec/shoulda/active_record/serialize_matcher_spec.rb
@@ -0,0 +1,62 @@
+require 'spec_helper'
+
+describe Shoulda::Matchers::ActiveRecord::SerializeMatcher do
+ context "an attribute that should be serialized" do
+ before do
+ define_model :example, :attr => :string do
+ serialize :attr
+ end
+ @model = Example.new
+ end
+
+ it "should be serialized" do
+ @model.should serialize(:attr)
+ end
+ end
+
+ context "an attribute that should be serialized with a type of Hash" do
+ before do
+ define_model :example, :attr => :string do
+ serialize :attr, Hash
+ end
+ @model = Example.new
+ end
+
+ it "should be serialized" do
+ @model.should serialize(:attr).as(Hash)
+ end
+ end
+
+ context "an attribute that should be serialized with a type of Array" do
+ before do
+ define_model :example, :attr => :string, :attr2 => :string do
+ serialize :attr, Array
+ serialize :attr2, Array
+ end
+ @model = Example.new
+ end
+
+ it "should be serialized" do
+ @model.should serialize(:attr).as(Array)
+ end
+ end
+
+ context "an attribute that should be serialized but isn't" do
+ before do
+ define_model :example, :attr => :string
+ @model = Example.new
+ end
+
+ it "should assign a failure message" do
+ matcher = serialize(:attr)
+ matcher.matches?(@model).should == false
+ matcher.failure_message.should_not be_nil
+ end
+
+ it "should assign a failure message with 'as'" do
+ matcher = serialize(:attr).as(Hash)
+ matcher.matches?(@model).should == false
+ matcher.failure_message.should_not be_nil
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.