Veerasundaravel's Ruby on Rails Weblog

July 25, 2012

Rspec Shoulda-Matcher made Unit testing as easier one

Filed under: Rails3, Ruby, Ruby On Rails, Testing, Unit Test — Tags: , , , , , , — Veerasundaravel @ 12:37 am

Shoulda matcher is a library that enables to write better and more understandable tests for Rails application. It is Test::Unit- and RSpec-compatible one-liners that test common Rails functionality.

Here few very easier example of shoulda-matchers for Unit Testing.

Test model fields:

it {should have_db_column(:login)}
it {should have_db_column(:salary).of_type(:decimal).with_options(:precision => 10, :scale => 2) }
it { should_not have_db_column(:admin).of_type(:boolean) }

Test db indexes:

it { should have_db_index(:age) }
it { should have_db_index([:commentable_type, :commentable_id]) }
it { should have_db_index(:ssn).unique(true) }

Test validations:

it { should validate_uniqueness_of(:title) }
it { should validate_presence_of(:body).with_message(/Enter the message/) }
it { should validate_presence_of(:title) }
it { should validate_numericality_of(:user_id) }
it { should validate_uniqueness_of(:title) }
it { should_not allow_value("blah").for(:email) }
it { should allow_value("").for(:email) }
it { should ensure_inclusion_of(:age).in_range(1..100) }
it { should_not allow_mass_assignment_of(:password) }

Test associations:

it { should belong_to(:parent) }
it { should have_one(:car)
it { should have_many(:friends) }
it { should have_many(:enemies).through(:friends) }


Further reading:

Shoulda-Matcher home page –
Shoulda-context home page –
rspec_shoulda cheat sheet –


October 7, 2010

Useful SVN and Git commands – Cheatsheet

More useful source control system (SVN & GIT) commands for your reference.

svn checkout/co checkout-url – used to pull an SVN tree from the server.
svn update/up – Used to update the local copy with the changes made in the repository.
svn commit/ci – m “message” filename – Used to commit the changes in a file to repository with a message.
svn diff filename – shows up the differences between your current file and what’s there now in the repository.
svn revert filename – To overwrite local file with the one in the repository.
svn add filename – For adding a file into repository, you should commit your changes then only it will reflect in repository.
svn delete filename – For deleting a file from repository, you should commit your changes then only it will reflect in repository.
svn move source destination – moves a file from one directory to another or renames a file. It will effect your local copy immediately as well as on the repository after committing.

git config – Sets configuration values for your user name, email, file formats and more.
git init – Initializes a git repository – creates the initial ‘.git’ directory in a new or in an existing project.
git clone – Makes a Git repository copy from a remote source. Also adds the original location as a remote so you can fetch from it again and push to it if you have permissions.
git add – Adds files changes in your working directory to your index.
git rm – Removes files from your index and your working directory so they will not be tracked.
git commit – Takes all of the changes written in the index, creates a new commit object pointing to it and sets the branch to point to that new commit.
git status – Shows you the status of files in the index versus the working directory.
git branch – Lists existing branches, including remote branches if ‘-a’ is provided. Creates a new branch if a branch name is provided.
git checkout – Checks out a different branch – switches branches by updating the index, working tree, and HEAD to reflect the chosen branch.
git merge – Merges one or more branches into your current branch and automatically creates a new commit if there are no conflicts.
git reset – Resets your index and working directory to the state of your last commit.
git tag – Tags a specific commit with a simple, human readable handle that never moves.
git pull – Fetches the files from the remote repository and merges it with your local one.
git push – Pushes all the modified local objects to the remote repository and advances its branches.
git remote – Shows all the remote versions of your repository.
git log – Shows a listing of commits on a branch including the corresponding details.
git show – Shows information about a git object.
git diff – Generates patch files or statistics of differences between paths or files in your git repository, or your index or your working directory.
gitk – Graphical Tcl/Tk based interface to a local Git repository.

Alos refer:

Useful Rails Commands & Cheatsheet
Useful Rails3 Commands & Cheatsheet

%d bloggers like this: