Comparing Hashes with RSpec
When you are working with rails and write tests with rspec, a lot of times you’ll need to compare hashes.
Lets say have a service that creates a new instance of our model:
Now we would like to test if key and name are set correctly. Rspec actually does pretty well when comparing hashes:
But wait, the test fails! Why? Because attributes have string keys. To get the test passing, just stringify the keys of our model:
Yeay, its all green now. You can find more information about the include matcher here