Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add checking for attribute aliases

  • Loading branch information...
commit dc632fb9df19a4d9bd9d8d12a43d74f6f6f28501 1 parent ed8f332
@alexspeller alexspeller authored
View
12 lib/active_model_serializers/matchers.rb
@@ -80,7 +80,7 @@ def embed(value)
end
class HaveAttribute
- attr_accessor :name, :actual
+ attr_accessor :name, :actual, :expected_key
def initialize(name)
@name = name
@@ -89,7 +89,9 @@ def initialize(name)
def matches?(actual)
@actual = actual
- attributes.has_key?(name)
+ return false unless attributes.has_key?(name)
+ return true unless expected_key
+ attributes[name] == expected_key
end
def description
@@ -103,6 +105,11 @@ def failure_message_for_should
def failure_message_for_should_not
%Q{expected #{actual.inspect} to not include: "#{name}", but it did}
end
+
+ def as(value)
+ self.expected_key = value
+ self
+ end
private
def serializer
@@ -121,6 +128,7 @@ def attributes
def have_attribute(name)
HaveAttribute.new name
end
+
class AssociationMatcher
attr_accessor :name, :actual, :expected_key
View
20 spec/active_model_serializers_matcher_spec.rb
@@ -11,6 +11,26 @@
serializer.should_not have_attribute :bar
serializer.should have_attribute :foo
end
+
+ it "should match singular attributes" do
+ serializer = Class.new ActiveModel::Serializer do
+ attribute :foo
+ end
+
+ serializer.should_not have_attribute :bar
+ serializer.should have_attribute :foo
+ end
+
+ it "should match attributes with keys" do
+ serializer = Class.new ActiveModel::Serializer do
+ attribute :foo, :key => :foo_name
+ end
+
+ serializer.should_not have_attribute :bar
+ serializer.should have_attribute :foo
+ serializer.should have_attribute(:foo).as(:foo_name)
+ serializer.should_not have_attribute(:foo).as(:another_name)
+ end
it "should match the embed setting" do
serializer = Class.new ActiveModel::Serializer do
Please sign in to comment.
Something went wrong with that request. Please try again.