views:

54

answers:

2

I've looked at the rails guide to testing but I can seem to get fixture data from inside my test. I have a fixture:

one:
  pay_period: one
  employee: kent
  cpp: 50
  ei: 40
  tax: 100
  vacation_pay: 30

Then I have a test as per below (where EmployeePayPeriod is a model)

require 'test_helper'
class EmployeePayPeriodTest < ActiveSupport::TestCase

  # Replace this with your real tests.
  test "Calculate wages under 80 hours correctly" do
    p = EmployeePayPeriod(:one)
    assert true
  end
end

Obviously the above isn't working. I've looked at the guide. In the guide it uses a code snippet to illustrate how you get fixture data:

@user = users(:david).

It seems that 'users' isn't a model (because the normal convention is that it would singular and capitalized. So where does 'users' come from? If its autogenerated should there be a comparable 'employee_pay_periods' object available for me? (I've tried, but it doesn't seem to work).

Thanks

+1  A: 

I believe users is a convention based on the yml filename. In the example, they have a users.yml. So, to answer your question more directly, try whatever the name of your .yml fixture file is.

Something that might be easier in the long run, though, is something like FactoryGirl, Machinist, or fixjour

theIV
Thanks theIV.
Daniel
My pleasure. Good luck with the fixture'ing.
theIV
+2  A: 

The fixture needs to be in a .yml file whose name matches the table name (not the model name).

Assuming the table name for EmployeePayPeriod is employee_pay_periods, then your fixture needs to be in test/fixtures/employee_pay_periods.yml, and you should have an employee_pay_periods() method autogenerated for accessing the fixtures.

Make sure your fixtures are actually being loaded. Latest Rails will add the following line to test/test_helper.rb:

fixtures :all

This loads all fixtures before each test case. Make sure this line is there and not commented out.

showaltb