It Should Include Only One Of – An RSpec Matcher

I know RSpec tests should typically be implementation/data agnostic, but I ran into a situation in a unit test where I needed to make sure that the results of a query returned only one of the two records that were created during the setup but without knowing which record that would be ahead of time. This was due to the fact that the scoped finder returned a random record. So, I wrote a little matcher that lets me assert that a given array contains only one of a set of specified elements.

[gist id=1021142 file=include_only_one_of.rb]

You can use it like so:

[gist id=1021142 file=example_spec.rb]

Hargo Day was established to also recognize the human capacity for appreciation, wonder, and awe that can be achieved when small plastic things are arranged in a certain order near and around a cat

— Mayor Curtatone of Somerville, MA., in declaring Feb. 24, 2005 as “Hargo Day”, http://www.boston.com/news/local/massachusetts/articles/2005/02/25/tiny_takeoff_on_christo_proves_gateway_to_glory?pg=2