Factory Girlは、テスト用のモデルのインスタンスを簡単に作成するためのレール内の便利なフレームワークです。
factory_girlを使用すると、各モデルのプロトタイプをすばやく定義し、手元のテストにとって重要なプロパティを持つインスタンスを要求できます。
例(これもホームページから):
Factory.sequence :email do |n|
"somebody#{n}@example.com"
end
# Let's define a factory for the User model. The class name is guessed from the
# factory name.
Factory.define :user do |f|
# These properties are set statically, and are evaluated when the factory is
# defined.
f.first_name 'John'
f.last_name 'Doe'
f.admin false
# This property is set "lazily." The block will be called whenever an
# instance is generated, and the return value of the block is used as the
# value for the attribute.
f.email { Factory.next(:email) }
end
ユーザーが必要な場合は、
test_user = Factory(:user, :admin => true)
これにより、明示的に指定したadminプロパティを除いて、ファクトリプロトタイプで指定されたすべてのプロパティを持つユーザーが生成されます。また、電子メールファクトリメソッドは、呼び出されるたびに異なる電子メールを生成することに注意してください。
Javaに似たようなものを実装するのはかなり簡単なはずだと思いますが、車輪の再発明はしたくありません。
PS:私はJMockとEasyMocの両方について知っていますが、ここではモックフレームワークについて話していません。