Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor a couple of matchers to use rspec-expectation's new BaseMatcher

  • Loading branch information...
commit f963a5b9a8c4745a869a3c5c902018aeab8be1a6 1 parent c0cfb51
@dchelimsky dchelimsky authored
View
71 lib/rspec/rails/matchers/be_a_new.rb
@@ -1,40 +1,51 @@
-RSpec::Matchers.define :be_a_new do |model_klass|
- match do |actual|
- actual.is_a?(model_klass) && actual.new_record? && attributes_match?(actual)
- end
+module RSpec::Matchers
+ class BeANew
+ include BaseMatcher
- chain :with do |expected_attributes|
- attributes.merge!(expected_attributes)
- end
+ def matches?(actual)
+ super
+ actual.is_a?(expected) && actual.new_record? && attributes_match?(actual)
+ end
- failure_message_for_should do |actual|
- [].tap do |message|
- unless actual.is_a?(model_klass) && actual.new_record?
- message << "expected #{actual.inspect} to be a new #{model_klass.inspect}"
- end
- unless attributes_match?(actual)
- if unmatched_attributes.size > 1
- message << "attributes #{unmatched_attributes.inspect} were not set on #{actual.inspect}"
- else
- message << "attribute #{unmatched_attributes.inspect} was not set on #{actual.inspect}"
+ def with(expected_attributes)
+ attributes.merge!(expected_attributes)
+ self
+ end
+
+ def failure_message_for_should
+ [].tap do |message|
+ unless actual.is_a?(expected) && actual.new_record?
+ message << "expected #{actual.inspect} to be a new #{expected.inspect}"
end
- end
- end.join(' and ')
- end
+ unless attributes_match?(actual)
+ if unmatched_attributes.size > 1
+ message << "attributes #{unmatched_attributes.inspect} were not set on #{actual.inspect}"
+ else
+ message << "attribute #{unmatched_attributes.inspect} was not set on #{actual.inspect}"
+ end
+ end
+ end.join(' and ')
+ end
- def attributes
- @attributes ||= {}
- end
+ def attributes
+ @attributes ||= {}
+ end
- def attributes_match?(actual)
- attributes.stringify_keys.all? do |key, value|
- actual.attributes[key].eql?(value)
+ def attributes_match?(actual)
+ attributes.stringify_keys.all? do |key, value|
+ actual.attributes[key].eql?(value)
+ end
end
- end
- def unmatched_attributes
- attributes.stringify_keys.reject do |key, value|
- actual.attributes[key].eql?(value)
+ def unmatched_attributes
+ attributes.stringify_keys.reject do |key, value|
+ actual.attributes[key].eql?(value)
+ end
end
+
+ end
+
+ def be_a_new(model_klass)
+ BeANew.new(model_klass)
end
end
View
14 lib/rspec/rails/matchers/be_new_record.rb
@@ -1,5 +1,13 @@
-RSpec::Matchers.define :be_new_record do
- match do |actual|
- !actual.persisted?
+module RSpec::Matchers
+ class BeANewRecord
+ include BaseMatcher
+
+ def matches?(actual)
+ !actual.persisted?
+ end
+ end
+
+ def be_new_record
+ BeANewRecord.new
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.