Permalink
Browse files

Merge pull request #117 from indrekj/error-message

Raise more readable error message
  • Loading branch information...
2 parents 26c4317 + 8ec2747 commit 6b512b876bc2e3b8279b9e16c44bdab9221ea9e0 @solnic committed Oct 5, 2012
Showing with 11 additions and 2 deletions.
  1. +8 −1 lib/virtus/instance_methods.rb
  2. +3 −1 spec/unit/virtus/instance_methods/attributes_spec.rb
@@ -191,7 +191,14 @@ def set_defaults
#
# @api private
def set_attributes(attributes)
- ::Hash.try_convert(attributes).each do |name, value|
+ hash = ::Hash.try_convert(attributes)
+
+ if hash.nil?
+ raise NoMethodError,
+ "Expected #{attributes.inspect} to respond to #to_hash"
+ end
+
+ hash.each do |name, value|
set_attribute(name, value) if allowed_writer_methods.include?("#{name}=")
end
end
@@ -131,7 +131,9 @@ def to_hash
let(:attribute_values) { '' }
it 'raises an exception' do
- expect { subject }.to raise_error(NoMethodError)
+ expect {
+ subject
+ }.to raise_error(NoMethodError, 'Expected "" to respond to #to_hash')
end
end
end

0 comments on commit 6b512b8

Please sign in to comment.