Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stratigos/ac64c1af62062f633b96009701fcf15f to your computer and use it in GitHub Desktop.
Save stratigos/ac64c1af62062f633b96009701fcf15f to your computer and use it in GitHub Desktop.
RSpec Shared Example Matcher for Class Constant Definition
# Shared specs for models with frozen CONST values. Expects two String values
# as parameters.
RSpec.shared_examples :has_const_model do |const_name, const_value|
it { expect(described_class).to be_const_defined(const_name.to_sym) }
it { expect("#{described_class}::#{const_name}".constantize).to be_frozen }
it { expect("#{described_class}::#{const_name}".constantize).to eq(const_value) }
end
@stratigos
Copy link
Author

stratigos commented Apr 19, 2018

This is for expressing a specification for a Ruby Class that holds a "const" value. E.g.,

class DoStuffService

  OFTEN_ACCESSED_STRING = "I use this value all over the place".freeze

  ...

end

...and you would like that expressed in your specs :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment