Last Updated: February 25, 2016
·
539
· rongworks

Declare shared attributes in rails fixtures

Declare shared attributes in rails fixtures using a base fixture.
user_fixture.yaml:

# base fixture, attributes are loaded into other fixtures   
base_user: &BASE_USER
  name: testy testicle
  mail: some_mail@example.com
  gender: male

user1:
  <<: *BASE_USER
  avatar: some_avatar #add avatar
  admin: false #add admin

user2:
  <<: *BASE_USER
  admin:true #add admin
  gender: female #overwrite gender

Helps defining base attributes (e.g. for validations) while concentrentrating on test-specific attributes in other fixtures.