Fixtures in Rails 2

This entry was posted on Wed, 21 May 2008 06:37:00 GMT . You can follow any any response to this entry through the Atom feed. You can leave a comment .

As part of my attempts to upgrade tinySIS to Rails 2, I am updating the test fixtures. I am starting with a basic set of fixtures to cover subject areas, contracts, users, enrollments, and settings. I’ll expand this fixture set later, but the goal right now is to get a test database up and limping so I can write a basic set of functional smoke-tests to verify the app before I deploy. Kind of sad, as the app has been in production for almost two school years now - and despite the lack of automated testing, it’s quite stable - but I am trying to move it in the 20th century.

I found a couple good resources on the new fixtures stuff:

Thanks, Ryans.

I won’t reiterate the great info in the above postings. I will just show you excerpts from my starter fixture set. I started by dumping my old test database to yml (the database was created using fixture_scenarios, which I did get to somewhat-work under Rails 2 but decided to go with the native Rails fixtures instead).

My mythological school is kind of inbred.

First, a set of contract categories:

homeroom:
  sequence: 100
  category_name: Homeroom
  public: 0
  statusable: 1
  publicly_enrollable: 0
independent:
  sequence: 300
  category_name: Independent
  public: 0
  statusable: 1
  publicly_enrollable: 0
social_studies: 
  sequence: 300
  category_name: Social Studies
  public: 1
  statusable: 1
  publicly_enrollable: 1
language_arts: 
  sequence: 300
  category_name: Language Arts
  public: 1
  statusable: 1
  publicly_enrollable: 1
seminar: 
  sequence: 200
  category_name: Seminar
  public: 0
  statusable: 2
  publicly_enrollable: 0

Now, a teacher (Micah Fester) and his offspring students (Frank, Filbert, Floyd, Francine, and, last but not least, Foobar):

<%
salt='abDVsxLO'
hash='96f2e53c290bbd380a121715336a7cdf50adc2ed632a4d88e6bb45bd2842dd92'
%>
fester:
  coordinator_id: 
  password_salt: <%= salt %>
  password_hash: <%= hash %>
  login: fester
  first_name: Micah
  last_name: Fester
  login_status: 2
  user_status: 1
  privilege: 2
  email: micah@fester.com
<%
  %w{frank filbert francine floyd foobar}.each do |u|
%>
fester_<%= u %>:
  coordinator: fester 
  nickname: <%= u %>
  password_salt: <%= salt %>
  password_hash: <%= hash %>
  login: fester_<%= u %>
  first_name: <%= u.capitalize %>
  last_name: Fester
  login_status: 2
  user_status: 1
  privilege: 1
  email: <%= u %>_fester@fester.com
<%
  end
%>

Now, the contract fixtures. They reference both the user and category (e.g. creator, revisor, and facilitator; and category) – using the fixture names which are converted to the appropriate database ID:

<% %w{homeroom language_arts social_studies independent seminar}.each do |c| %>
fester_<%= c %>:
  name: Fester <%= c.capitalize %>
  term: fall
  creator: admin
  facilitator: fester
  category: <%= c %>
  contract_status: 1
  revisor: admin
<% end %>

Finally, to connect users (students) to contracts, the enrollment fixtures:

<%
  %w{frank filbert francine floyd foobar}.each do |u|
    %w{homeroom math science independent seminar}.each do |c|
%>
enrollment_<%= u %>_fester_<%= c %>: 
  participant: fester_<%= u %>
  creator: admin
  completion_status: 0
  role: 0
  finalized_on: 
  completion_date: 
  enrollment_status: 1
  contract: fester_<%= c %>
  revisor: admin
<%
    end
  end
%>

Load it up:

rake db:fixtures:load RAILS_ENV=test --trace

And here we are! Shiny.

Micah Fester's homeroom


Leave a comment