Rails - how to grab instrument data from tests

I looked at rails for testing , but I can get hardware data from 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 like below (where EmployeePayPeriod is the 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 this doesn't work. I looked at the guide. In the tutorial, he uses a code snippet to illustrate how you get hardware data:

@user = users(:david).  

      

It looks like "users" are not a model (because the usual convention is that it would be singular and capitalized. So where did the "users" come from? If its autogeneration should have a comparable employee_pay_periods object available for (I tried, but doesn't seem to work).

thank

+2


source to share


2 answers


I believe users are a convention based on the yml file name. In this example, they matter users.yml

. Therefore, to more accurately answer your question, try specifying the name of your .yml fixture file.



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

+3


source


The instrument must 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 device should be in test/fixtures/employee_pay_periods.yml

, and you should have a method employee_pay_periods()

autogenerated to access the devices.

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



fixtures :all

      

This loads all fixtures before each test case. Make sure this line is present and uncommented.

+6


source







All Articles