Skip to content

Instantly share code, notes, and snippets.

@futhr
Forked from luisalima/shoulda_cheat_model.rb
Last active December 16, 2015 03:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save futhr/5373054 to your computer and use it in GitHub Desktop.
Save futhr/5373054 to your computer and use it in GitHub Desktop.
# updated from the original @ http://cheat.errtheblog.com/s/rspec_shoulda
# just a subset -- models -- is included here. I'll update this, and create cheat sheets for others, as I go along.
# I marked the ones I added with NEW and also added the links to the corresponding code, as I think it's useful.
# Any comments/corrections are welcome!
# ================= Data and Associations =======================
# https://github.com/thoughtbot/shoulda-matchers/tree/master/lib/shoulda/matchers/active_record
it { is_expected.not_to have_db_column(:admin).of_type(:boolean) }
it { is_expected.to have_db_column(:salary).
of_type(:decimal).
with_options(precision: 10, scale: 2) }
it { is_expected.to have_readonly_attributes(:password) }
it { is_expected.to belong_to(:parent) }
it { is_expected.to have_db_index(:id) }
it { is_expected.to have_many(:friends) }
it { is_expected.to have_many(:enemies).through(:friends) }
it { is_expected.to have_many(:enemies).dependent(:destroy) }
it { is_expected.to have_one(:god) }
it { is_expected.to have_and_belong_to_many(:posts) }
it { is_expected.to accept_nested_attributes_for(:user) }
# ================== Validation Matchers ============================
# https://github.com/thoughtbot/shoulda-matchers/tree/master/lib/shoulda/matchers/active_model
it { is_expected.to validate_uniqueness_of(:keyword) }
it { is_expected.to validate_uniqueness_of(:keyword).with_message(/dup/) }
it { is_expected.to validate_uniqueness_of(:email).scoped_to(:name) }
it { is_expected.to validate_uniqueness_of(:email).
scoped_to(:first_name, :last_name) }
it { is_expected.to validate_uniqueness_of(:keyword).case_insensitive }
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_presence_of(:name).
with_message(/is not optional/) }
it { is_expected.to validate_numericality_of(:age) }
it { is_expected.to validate_format_of(:name).
with('12345').
with_message(/is not optional/) }
it { is_expected.to validate_format_of(:name).
not_with('12D45').
with_message(/is not optional/) }
it { is_expected.to validate_acceptance_of(:eula) }
it { is_expected.to ensure_length_of(:password).
is_at_least(6).
is_at_most(20) }
it { is_expected.to ensure_length_of(:name).
is_at_least(3).
with_short_message(/not long enough/) }
it { is_expected.to ensure_length_of(:ssn).
is_equal_to(9).
with_message(/is invalid/) }
it { is_expected.to ensure_inclusion_of(:age).in_range(0..100) }
it { is_expected.to validate_format_of(:first_name).with('Carl') }
it { is_expected.to validate_confirmation_of(:password) }
it { is_expected.to allow_value(true).for(:is_on_vacation) }
it { is_expected.to allow_value(false).for(:is_on_vacation) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment