Skip to content

Instantly share code, notes, and snippets.

@dandrews
Forked from backpackerhh/have_constant.rb
Created August 4, 2016 20:13
Show Gist options
  • Save dandrews/1e006ef27919a2ec739c4d0d8bc84663 to your computer and use it in GitHub Desktop.
Save dandrews/1e006ef27919a2ec739c4d0d8bc84663 to your computer and use it in GitHub Desktop.
Custom Rspec matcher for checking class or module for constant. Based on a David Chelimsky's answer on StackOverflow.
# spec/support/matchers/have_constant.rb
RSpec::Matchers.define :have_constant do |constant|
match do |owner|
([Class, Module].include?(owner.class) ? owner : owner.class).const_defined?(constant)
end
failure_message_for_should do |klass|
"expected #{klass} to have constant #{constant}"
end
failure_message_for_should_not do |klass|
"expected #{klass} not to have constant #{constant}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment