Skip to content

Instantly share code, notes, and snippets.

@uhlenbrock
Created September 23, 2008 20:05
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 uhlenbrock/12380 to your computer and use it in GitHub Desktop.
Save uhlenbrock/12380 to your computer and use it in GitHub Desktop.
Sample Unit Tests
class User < ActiveRecord::Base
# Validation
validates_presence_of :password_confirmation
# Association
has_many :comments
# Instance Method
def full_name
"#{first_name} #{last_name}"
end
end
context "A User" do
# Validation specification
specify "should require password confirmation" do
u = create_user(:password_confirmation => nil)
u.should.not.validate
u.errors.on(:password_confirmation).should.not.be.blank
end
# Association specification
specify "should know about his comments" do
u = create_user(
:comments => [
{ :body => 'you rock!' },
{ :body => 'i know it!' }
])
u.comments.count.should.be 2
end
# Instance method specification
specify "should format full name properly" do
u = create_user(:first_name => "George", :last_name => "Michael")
u.full_name.should.equal "#{u.first_name} #{u.last_name}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment