New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
include() doesn't work with ActiveModel::Errors #1367
Comments
What output do you currently get? |
Currently pairing, will get some output when I can. |
The error message is:
The issue is def include?(attribute)
messages[attribute].present?
end Which only checks if the attribute is a key for the messages. Thus the diff says it has Also, note that expect(errors.messages).to include(name: ["can't be blank"])
expect(errors.messages).to include(name: including("can't be blank")) |
The root issue is that the errors.is_a?(Hash)
# => false However, it acts as a hash. More specifically, it contains custom implementations for many methods (such as @myronmarston Perhaps we could consider improving the matcher to check both |
I thought that http://blog.marc-andre.ca/2013/02/23/ruby-2-by-example/#to_h I've implemented Regardless, relying on
Thus we can't assume it'll always work. Given the complexities around coercing to a hash, I think going that direction is potentially problematic. Maybe we can improve the
...we include a note in the failure message about the fact that the object is not a hash and they may need to convert it to one. |
My understanding is there are two protocols:
In Ruby 1.8.7 only Explicit is for when a thing, can potentially convert to the core type. Implicit is for when a thing, can be the actual core type. I think the main source I got this from is Avdi's "Confident Ruby". |
Huh, I never new about that. Sounds like |
Yeah that's news to me. On Sat, May 16, 2015 at 3:59 PM, Myron Marston notifications@github.com
Kurtis Rainbolt-Greene, Hacker |
Started looking into implementing this. It's a bit trickier than I initially anticipated due to the matcher being diffable. I have it mostly working. On a failing example the output is:
Though I'd prefer it to be:
Just debating implementation details I guess. |
Shouldn't it be? expect(model.errors.messages).to include(name: include("can't be blank")) |
Its always several messages IIRC |
You're right 👍 Fixed |
The original issue is because the errors object is hash like, but not a hash, so this is one of our matchers that could (should?) be improved in rspec-rails |
The error might not be quite that, but basically that.
The text was updated successfully, but these errors were encountered: