Skip to content

Instantly share code, notes, and snippets.

@mmower
Created September 20, 2009 15:15
Show Gist options
  • Save mmower/189820 to your computer and use it in GitHub Desktop.
Save mmower/189820 to your computer and use it in GitHub Desktop.
class Person
attr_accessor :name, :age, :gender
end
person_instance = Person.new
person_instance.name = 'robert'
person_instance.age = 52
person_instance.gender = 'male'
puts person_instance.name
puts person_instance.age
puts person_instance.gender
require 'test/unit'
class TestPerson < Test::Unit::TestCase
def setup
@prsn = Person.new
end
def test_name
assert_equal "robert", prsn.name
end
def test_age
assert_equal 52, prsn.age
end
def test_gender
assert_equal 'male', prsn.gender
end
end
You've defined accessors for name, age, and gender so use them rather than directly accessing the ivars using @. It's not just good form, it pays off in the long run when the concept those ivars represent change somehow.
Also when doing direct comparisons it's better to use assert_equal because you get a better error message when they aren't.
Lastly I would also consider defining,
class Person
def male?; gender == :male; end
def female?; gender == :female; end
end
and write the last test as:
def test_gender
assert prsn.male?
end
But it's largely a matter of taste at that point.
M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment