Today I Learned

A Hashrocket project

Ready to join Hashrocket? Find Openings here and apply today.

Custom RSpec negation matchers

You can create negated versions of existing rspec matchers, mostly useful with composition.

# spec/rails_helper.rb
RSpec::Matchers.define_negated_matcher :an_array_excluding, :include
# spec.rb
list = (1..5).to_a
expect {
  list.delete(5)
}.to change { 
  list
}.to an_array_excluding(5)
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.