Skip to content

Instantly share code, notes, and snippets.

@sameera207
Created December 18, 2012 10:24
Show Gist options
  • Save sameera207/4326908 to your computer and use it in GitHub Desktop.
Save sameera207/4326908 to your computer and use it in GitHub Desktop.
---
rspec_shoulda: |
Shoulda Rspec Matchers
For more information on rspec, rspec-rails, shoulda
$ cheat rspec
$ cheat rspec_on_rails_matchers
$ cheat shoulda
Models Matchers
Data
it { should_not have_db_column(:admin).of_type(:boolean) }
it { should have_db_column(:salary).
of_type(:decimal).
with_options(:precision => 10, :scale => 2) }
Associations
it { should belong_to(:parent) }
it { should have_many(:friends) }
it { should have_many(:enemies).through(:friends) }
it { should have_many(:enemies).dependent(:destroy) }
it { should have_one(:god) }
it { should have_and_belong_to_many(:posts) }
Validation Matchers
it { should validate_uniqueness_of(:keyword) }
it { should validate_uniqueness_of(:keyword).with_message(/dup/) }
it { should validate_uniqueness_of(:email).scoped_to(:name) }
it { should validate_uniqueness_of(:email).
scoped_to(:first_name, :last_name) }
it { should validate_uniqueness_of(:keyword).case_insensitive }
it { should validate_presence_of(:name) }
it { should validate_presence_of(:name).
with_message(/is not optional/) }
it { should validate_numericality_of(:age) }
it { should validate_format_of(:name).
with('12345').
with_message(/is not optional/) }
it { should validate_format_of(:name).
not_with('12D45').
with_message(/is not optional/) }
it { should validate_acceptance_of(:eula) }
it { should have_readonly_attributes(:password) }
it { should ensure_length_of(:password).
is_at_least(6).
is_at_most(20) }
it { should ensure_length_of(:name).
is_at_least(3).
with_short_message(/not long enough/) }
it { should ensure_length_of(:ssn).
is_equal_to(9).
with_message(/is invalid/) }
it { should ensure_inclusion_of(:age).in_range(0..100) }
it { should_not allow_mass_assignment_of(:password) }
it { should allow_mass_assignment_of(:first_name) }
Controller Matchers
Assigns
it { should assign_to(:user) }
it { should assign_to(:user) }
it { should_not assign_to(:user) }
it { should assign_to(:user).with_kind_of(User) }
it { should assign_to(:user).with(@user) }
Sessions
it { should set_session(:message) }
it { should set_session(:user_id).to(@user.id) }
it { should_not set_session(:user_id) }
Flash
it { should set_the_flash }
it { should set_the_flash.to("Thank you for placing this order.") }
it { should set_the_flash.to(/created/i) }
it { should_not set_the_flash }
Responses
it { should respond_with(:success) }
it { should respond_with(:redirect) }
it { should respond_with(:missing) }
it { should respond_with(:error) }
it { should respond_with(501) }
Content-Type Responses
it { should respond_with_content_type(:xml) }
it { should respond_with_content_type(:csv) }
it { should respond_with_content_type(:atom) }
it { should respond_with_content_type(:yaml) }
it { should respond_with_content_type(:text) }
it { should respond_with_content_type('application/rss+xml') }
it { should respond_with_content_type(/json/) }
Rendering
it { should render_template(:new) }
it { should render_with_layout }
it { should render_with_layout(:special) }
it { should_not render_with_layout }
Filter Parameter
it { should filter_param(:password) }
Route Matchers
it { should route(:get, "/posts/new").to(:action => :new) }
it { should route(:put, "/posts/1").to(:action => :update, :id => 1) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment