Created
September 23, 2008 20:05
-
-
Save uhlenbrock/12380 to your computer and use it in GitHub Desktop.
Sample Unit Tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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