Hi,
--- On Sun, Jul 11, 2010 at 9:27 AM, Shakthi Kannan <shakthimaan(a)gmail.com>
| wrote:
|
| Gaurav:
|
| Use factory_girl?
|
| http://github.com/thoughtbot/factory_girl
\--
Just to add to the above, after installing the factory_girl gem,
create a directory test/factories in the project sources. For each
model, create a file <model>.rb. For example:
=== test/factories/event.rb ===
Factory.define :event do |u|
u.name 'Testing'
u.start_at '2010-07-25 00:00:00'
u.end_at '2010-07-29 00:00:00'
u.created_at '2010-07-16 00:00:00'
u.updated_at '2010-07-16 00:00:00'
end
=== END ===
In your unit tests, you can create an event object using:
event = Factory.build(:event)
If you want to override any attributes of the event object, you can
pass it as a parameter:
event = Factory.build(:event, :name => 'Migration')
For every code, constraint that you put in the model, you should have
a test case that addresses it. When you modify any model code in
future, you will need to re-run the test cases to make sure you didn't
break anything. In future, as you write code, make sure you have a
test case for it.
The test cases are also helpful when you move into normalizing, or
optimize or re-factoring your code, as you can re-run the test cases
to make sure your changes haven't modified any logic.
SK
--
Shakthi Kannan
http://www.shakthimaan.com