Permalink
Browse files

HaveAttributeMatcher failure messages now use an expected/got format

  • Loading branch information...
1 parent db706b4 commit 39c4bc707a1560e24d4cdf59ad69b1c800013c7d @cgriego committed Dec 30, 2012
View
4 CHANGELOG.md
@@ -1,3 +1,7 @@
+# ActiveAttr 0.7.1 (Unreleased) #
+
+* HaveAttributeMatcher failure messages now use an expected/got format
+
# ActiveAttr 0.7.0 (December 15, 2012) #
* Added Serialization
View
21 lib/active_attr/matchers/have_attribute_matcher.rb
@@ -1,3 +1,5 @@
+require "active_attr/attribute_definition"
+
module ActiveAttr
module Matchers
# Specify that a model should have an attribute matching the criteria. See
@@ -30,9 +32,10 @@ class HaveAttributeMatcher
def initialize(attribute_name)
raise TypeError, "can't convert #{attribute_name.class} into Symbol" unless attribute_name.respond_to? :to_sym
@attribute_name = attribute_name.to_sym
+ @attribute_options = {}
@description = "attribute named #{attribute_name}"
@expected_ancestors = ["ActiveAttr::Attributes"]
- @attribute_expectations = [lambda { attribute_definition }]
+ @attribute_expectations = [lambda { actual_attribute_definition }]
end
# Specify that the attribute should have the given type
@@ -48,6 +51,7 @@ def initialize(attribute_name)
#
# @since 0.5.0
def of_type(type)
+ @attribute_options[:type] = type
@description << " of type #{type}"
@expected_ancestors << "ActiveAttr::TypecastedAttributes"
@attribute_expectations << lambda { @model_class._attribute_type(attribute_name) == type }
@@ -69,9 +73,10 @@ def of_type(type)
#
# @since 0.5.0
def with_default_value_of(default_value)
+ @attribute_options[:default] = default_value
@description << " with a default value of #{default_value.inspect}"
@expected_ancestors << "ActiveAttr::AttributeDefaults"
- @attribute_expectations << lambda { attribute_definition[:default] == default_value }
+ @attribute_expectations << lambda { actual_attribute_definition[:default] == default_value }
self
end
@@ -92,6 +97,9 @@ def matches?(model_or_model_class)
def failure_message
if missing_ancestors.any?
"expected #{@model_class.name} to include #{missing_ancestors.first}"
+ elsif actual_attribute_definition
+ "expected: #{expected_attribute_definition.inspect}\n" +
+ " got: #{actual_attribute_definition.inspect}"
else
"expected #{@model_class.name} to have #{@description}"
end
@@ -100,15 +108,20 @@ def failure_message
# @return [String] Negative failure message
# @private
def negative_failure_message
- "expected #{@model_class.name} to not have #{@description}"
+ "expected not: #{expected_attribute_definition.inspect}\n" +
+ " got: #{actual_attribute_definition.inspect}"
end
private
- def attribute_definition
+ def actual_attribute_definition
@model_class.attributes[attribute_name]
end
+ def expected_attribute_definition
+ AttributeDefinition.new @attribute_name, @attribute_options
+ end
+
def missing_ancestors
model_ancestor_names = @model_class.ancestors.map(&:name)
View
113 spec/functional/active_attr/matchers/have_attribute_matcher_spec.rb
@@ -3,6 +3,7 @@
require "active_attr/attribute_defaults"
require "active_attr/matchers/have_attribute_matcher"
require "active_attr/typecasted_attributes"
+require "active_support/core_ext/string/strip"
module ActiveAttr
module Matchers
@@ -98,7 +99,12 @@ def self.name
describe "#negative_failure_message" do
before { subject.matches?(model_class) }
- it { subject.negative_failure_message.should == %{expected Person to not have attribute named first_name} }
+ it do
+ subject.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ expected not: attribute :first_name
+ got: attribute :first_name
+ MESSAGE
+ end
end
end
@@ -131,7 +137,12 @@ def self.name
describe "#failure_message" do
before { subject.matches?(model_class) }
- it { subject.failure_message.should == %{expected Person to have attribute named first_name with a default value of "John"} }
+ it do
+ subject.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ expected: attribute :first_name, :default => "John"
+ got: attribute :first_name
+ MESSAGE
+ end
end
end
@@ -145,7 +156,12 @@ def self.name
describe "#failure_message" do
before { subject.matches?(model_class) }
- it { subject.failure_message.should == %{expected Person to have attribute named first_name with a default value of "John"} }
+ it do
+ subject.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ expected: attribute :first_name, :default => "John"
+ got: attribute :first_name, :default => "Doe"
+ MESSAGE
+ end
end
end
@@ -159,7 +175,12 @@ def self.name
describe "#negative_failure_message" do
before { subject.matches?(model_class) }
- it { subject.negative_failure_message.should == %{expected Person to not have attribute named first_name with a default value of "John"} }
+ it do
+ subject.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ expected not: attribute :first_name, :default => "John"
+ got: attribute :first_name, :default => "John"
+ MESSAGE
+ end
end
end
end
@@ -180,7 +201,12 @@ def self.name
describe "#failure_message" do
before { subject.matches?(model_class) }
- it { subject.failure_message.should == %{expected Person to have attribute named admin with a default value of false} }
+ it do
+ subject.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ expected: attribute :admin, :default => false
+ got: attribute :admin
+ MESSAGE
+ end
end
end
@@ -194,7 +220,12 @@ def self.name
describe "#failure_message" do
before { subject.matches?(model_class) }
- it { subject.failure_message.should == %{expected Person to have attribute named admin with a default value of false} }
+ it do
+ subject.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ expected: attribute :admin, :default => false
+ got: attribute :admin, :default => nil
+ MESSAGE
+ end
end
end
@@ -208,7 +239,12 @@ def self.name
describe "#negative_failure_message" do
before { subject.matches?(model_class) }
- it { subject.negative_failure_message.should == %{expected Person to not have attribute named admin with a default value of false} }
+ it do
+ subject.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ expected not: attribute :admin, :default => false
+ got: attribute :admin, :default => false
+ MESSAGE
+ end
end
end
end
@@ -229,7 +265,12 @@ def self.name
describe "#negative_failure_message" do
before { subject.matches?(model_class) }
- it { subject.negative_failure_message.should == %{expected Person to not have attribute named first_name with a default value of nil} }
+ it do
+ subject.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ expected not: attribute :first_name, :default => nil
+ got: attribute :first_name
+ MESSAGE
+ end
end
end
@@ -243,7 +284,12 @@ def self.name
describe "#failure_message" do
before { subject.matches?(model_class) }
- it { subject.failure_message.should == %{expected Person to have attribute named first_name with a default value of nil} }
+ it do
+ subject.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ expected: attribute :first_name, :default => nil
+ got: attribute :first_name, :default => false
+ MESSAGE
+ end
end
end
@@ -257,7 +303,12 @@ def self.name
describe "#negative_failure_message" do
before { subject.matches?(model_class) }
- it { subject.negative_failure_message.should == %{expected Person to not have attribute named first_name with a default value of nil} }
+ it do
+ subject.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ expected not: attribute :first_name, :default => nil
+ got: attribute :first_name, :default => nil
+ MESSAGE
+ end
end
end
end
@@ -278,7 +329,12 @@ def self.name
describe "#failure_message" do
before { subject.matches?(model_class) }
- it { subject.failure_message.should == %{expected Person to have attribute named first_name of type String} }
+ it do
+ subject.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ expected: attribute :first_name, :type => String
+ got: attribute :first_name
+ MESSAGE
+ end
end
end
@@ -292,7 +348,12 @@ def self.name
describe "#failure_message" do
before { subject.matches?(model_class) }
- it { subject.failure_message.should == %{expected Person to have attribute named first_name of type String} }
+ it do
+ subject.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ expected: attribute :first_name, :type => String
+ got: attribute :first_name, :type => Symbol
+ MESSAGE
+ end
end
end
@@ -306,7 +367,12 @@ def self.name
describe "#negative_failure_message" do
before { subject.matches?(model_class) }
- it { subject.negative_failure_message.should == %{expected Person to not have attribute named first_name of type String} }
+ it do
+ subject.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ expected not: attribute :first_name, :type => String
+ got: attribute :first_name, :type => String
+ MESSAGE
+ end
end
end
end
@@ -327,7 +393,12 @@ def self.name
describe "#negative_failure_message" do
before { subject.matches?(model_class) }
- it { subject.negative_failure_message.should == %{expected Person to not have attribute named first_name of type Object} }
+ it do
+ subject.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ expected not: attribute :first_name, :type => Object
+ got: attribute :first_name
+ MESSAGE
+ end
end
end
@@ -341,7 +412,12 @@ def self.name
describe "#failure_message" do
before { subject.matches?(model_class) }
- it { subject.failure_message.should == %{expected Person to have attribute named first_name of type Object} }
+ it do
+ subject.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ expected: attribute :first_name, :type => Object
+ got: attribute :first_name, :type => String
+ MESSAGE
+ end
end
end
@@ -355,7 +431,12 @@ def self.name
describe "#negative_failure_message" do
before { subject.matches?(model_class) }
- it { subject.negative_failure_message.should == %{expected Person to not have attribute named first_name of type Object} }
+ it do
+ subject.negative_failure_message.should == <<-MESSAGE.strip_heredoc.chomp
+ expected not: attribute :first_name, :type => Object
+ got: attribute :first_name, :type => Object
+ MESSAGE
+ end
end
end
end

0 comments on commit 39c4bc7

Please sign in to comment.