11/5/08
Test::Unit basic helpers
I've recently started writing unit tests again after fiddling with rspec for a while now and I decided to take the private crud approach internally. This involves creating a private method to do stuff like create a new record to test that result and so on. You create this in each unit. Then came the idea of testing finds and results from named scopes. Drop some more methods in to the unit test ... this is getting pretty wet. So I decided to dry it up a bit by centralizing these in the test_helper.
There were three needs that were immediate:
One of the things I found myself doing on point 2 and 3 was to check the class to make sure it was really a record. That involved something crude like:
in test/units/user_test.rb
So the first thing I need to look at in dry'ing this up is to get rid of the class name in the method ... Ok. Ruby's a dynamic language right? So here is my quick and dirty dry solution:
in test/test_helper.rb
in test/units/user_test.rb
Notice that the main difference is an extra argument when calling the validation helpers. I think it's worth it to keep this expandable, but we can even go dry'er if we wanted. With a little work we could dry up even the assert messages and everything. Just trying to keep those minds working. Cheers!
There were three needs that were immediate:
- assert a record was created
- assert a record was found
- and assert a list of records was found
One of the things I found myself doing on point 2 and 3 was to check the class to make sure it was really a record. That involved something crude like:
in test/units/user_test.rb
def test_should_create_user
user = create(:login => 'm3talsmith')
assert user.valid?, "User was not created: #{user}"
end
def test_should_find_users
users = User.find(:all)
assert contains_valid_users?(users), "Could not find a complete record of users: #{users}"
end
def test_should_find_user
user = User.find(:first)
assert is_valid_user?(user), "User was not valid: #{user}"
end
private
def create(options = {})
User.create(options)
end
def is_valid_user?(user)
return (user.class.to_s == "User")
end
def contains_valid_users?(users)
has_valid_users = true
users.each do |user|
if !is_valid_user?(user)
has_valid_users = false
break
end
end
return has_valid_users
end
So the first thing I need to look at in dry'ing this up is to get rid of the class name in the method ... Ok. Ruby's a dynamic language right? So here is my quick and dirty dry solution:
in test/test_helper.rb
private
def create(class_name = "Object", options = {})
eval(class_name.to_s).create(options)
end
def is_valid_class?(expected_class = "Object", class_item = Object.new)
return (class_item.class.to_s == expected_class.to_s)
end
def contains_valid_classes?(expected_class = "Object", class_items = [])
has_valid_class = true
class_items.each do |class_item|
if !is_valid_class?(expected_class, class_item)
has_valid_class = false
break
end
end
return has_valid_class
end
in test/units/user_test.rb
def test_should_create_user
user = create("User", :login => 'm3talsmith')
assert user.valid?, "User was not created: #{user}"
end
def test_should_find_users
user = User.find(:all)
assert contains_valid_classes?("User", users), "Could not find a complete record of users: #{users}"
end
def test_should_find_user
user = User.find(:first)
assert is_valid_class?("User", user), "User was not valid: #{user}"
end
Notice that the main difference is an extra argument when calling the validation helpers. I think it's worth it to keep this expandable, but we can even go dry'er if we wanted. With a little work we could dry up even the assert messages and everything. Just trying to keep those minds working. Cheers!
Labels: ruby on rails, testing



0 Comments:
Links to this post:
Create a Link
<< Home