Bulk testing your model attributes with rails
When it comes to testing in Ruby on Rails I tend to test every piece of its public interface. This means I test every single public method and attribute to ensure my desired functionality. Recently I’ve updated my application from Rails 1.2.5 to 2.0.1 and I was happy to have tests. None of them failed and so I knew that my application is ready to rock with the new Rails 2.0.
However, what I want to write in this article about is how to do bulk testing of each single attribute of your models. Let’s be a bit precise. Say we have a model User which has attributes firstname, lastnameand age. In order to perfectly test the User model we should write tests which …
- ensure that invalid values result in an error for the tested attribute (e.g. age -2 is invalid, so the instance should hold an error for the attribute
age) - and valid values don’t result in an error for the tested attribute (e.g. age 20 should be fine)
Usually you should test a couple of invalid values and a couple of valid values against each attribute of your model. This can be quite a lot to code. I explain a nice approach how to speed things up and save time for the next family event. (more…)


(3 votes, average: 4.67 out of 5)
FEED ME!