Koujou コウジョウ (sort of pronounced like "kojo", for us Gaijin) is yet another object mother fixture replacement for the Ruby on Rails framework, with one major difference: you don't have to define a thing! All the test data is generated on demand by Koujou through the magical power of Ruby.


gem install mleung-koujou --source http://gems.github.com

If you'd like to use the Rails Gem dependency manager, you'll need this in your environment.rb:

config.gem "mleung-koujou", :lib => "koujou", :source => "http://gems.github.com"


Create a model with the data saved in the datastore:
person = Person.koujou_create
Generate an initialized model, that hasn't been saved:
person = Person.koujou_build

You can even override any fields you want:

person = Person.koujou_create(:name => "まつもとゆきひろ")

A shortcut to koujou_create is:

person = Person.koujou

Note: the koujou method takes true or false as the first parameter (create = true, build = false), so if you want to pass in any attributes, you have to do:

car = Car.koujou(true, :make => "Subaru", :model => "Impreza WRX STI")

That's pretty much all there is to it. There's no need to define factories anywhere. You just call the koujou method and it returns a fully initialized active record model, ready for use in your tests/specs.


Koujou examines your model through reflection, assigns values for any required fields, and generates any required associations. One thing to be aware of is associated classes will not be created unless there's a validations_presence_of :model_id in your model.


Koujou was created by Michael Leung, after spending way too much time defining sample factory data.

The following people provided valuable input, and testing:

Also big shout-out to Michael Schuerig for his awesome validation_reflection plugin, parts of which were adapted, and incorporated into Koujou.

Continuous Integration by run code run.


Right now, Koujou doesn't really support validates_format_of. So if you have a field that gets validated with a complex regular expression, you should just pass some data in when calling koujou. That said, if your field is an email address, and has the word email in it, koujou will create a valid email address, thanks to Faker.


If you'd like to help out with Koujou, feel free to fork the master branch, and send me a pull request on Github.


Koujou is essentially brand new, and active_record is fairly complex. That means there are bound to be some edge cases that I haven't run across yet. If you find something wrong, and don't have the time to fork Koujou, and fix it yourself, please post the issue here.

What is a Koujou?

Koujou is the Japanese word for factory.