Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 3.15 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Ruby on Rails: related models functional test error
  2. 1.9.2-p290 :015 >   a = Allergy.all
  3.   Allergy Load (0.4ms)  SELECT "allergies".* FROM "allergies"
  4.  => [#<Allergy id: 1, name: "Milk", desc: "Allergic to milk", created_at: "2012-01-08 16:38:55", updated_at: "2012-01-09 11:48:20", patient_id: 1>, #<Allergy id: 2, name: "Blah", desc: "Test", created_at: "2012-01-09 12:20:48", updated_at: "2012-01-09 12:20:48", patient_id: 2>]
  5. 1.9.2-p290 :016 > a[0]
  6.  => #<Allergy id: 1, name: "Milk", desc: "Allergic to milk", created_at: "2012-01-08 16:38:55", updated_at: "2012-01-09 11:48:20", patient_id: 1>
  7. 1.9.2-p290 :017 > a[0].patient.full_name
  8.   Patient Load (0.5ms)  SELECT "patients".* FROM "patients" WHERE "patients"."id" = 1 LIMIT 1
  9.  => "Test Full Name"
  10. 1.9.2-p290 :018 >
  11.        
  12. class AllergiesController < ApplicationController
  13.   # GET /allergies
  14.   # GET /allergies.json
  15.   def index
  16.     @allergies = Allergy.all
  17.  
  18.     respond_to do |format|
  19.       format.html # index.html.erb
  20.       format.json { render json: @allergies }
  21.     end
  22.   end
  23.  
  24.   # GET /allergies/1
  25.   # GET /allergies/1.json
  26.   def show
  27.     @allergy = Allergy.find(params[:id])
  28.  
  29.     respond_to do |format|
  30.       format.html # show.html.erb
  31.       format.json { render json: @allergy }
  32.     end
  33.   end
  34.   ...
  35.        
  36. class Allergy < ActiveRecord::Base
  37.   validates :name, :presence => true
  38.   belongs_to :patient
  39. end
  40.        
  41. class Patient < ActiveRecord::Base
  42.   validates :first_name, :last_name, :dob, :presence => true
  43.   has_many :allergies
  44.  
  45.   def age
  46.     now = Time.now.utc.to_date
  47.     now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
  48.   end
  49.  
  50.   def full_name
  51.     first_name
  52.   end
  53. end
  54.        
  55. require 'test_helper'
  56.  
  57. class AllergiesControllerTest < ActionController::TestCase
  58.   setup do
  59.     @allergy = allergies(:one)
  60.     @allergy.patient = patients(:one)
  61.   end
  62.  
  63.   test "should get index" do
  64.     get :index
  65.     assert_response :success
  66.     assert_not_nil assigns(:allergies)
  67.   end
  68.   ...
  69.        
  70. # For Allergy
  71. one:
  72.   name: MyString
  73.   desc: MyText
  74.   patient_id: 1
  75.  
  76. two:
  77.   name: MyString
  78.   desc: MyText
  79.   patient_id: 1
  80.  
  81. #For Patient
  82. one:
  83.   first_name: Jose
  84.   last_name: Rizal
  85.   middle_name: H
  86.   dob: 2009-10-29
  87.  
  88. two:
  89.   first_name: MyString
  90.   last_name: MyString
  91.   middle_name: MyString
  92.   dob: 1982-02-11
  93.        
  94. AllergiesControllerTest:
  95.      PASS should create allergy (0.22s)
  96.      PASS should destroy allergy (0.01s)
  97.      PASS should get edit (0.13s)
  98.     ERROR should get index (0.14s)
  99.           ActionView::Template::Error: undefined method `full_name' for nil:NilClass
  100.           /Users/wenbert/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/whiny_nil.rb:48:in `method_missing'
  101.  
  102.      PASS should get new (0.02s)
  103.      PASS should show allergy (0.02s)
  104.      PASS should update allergy (0.02s)
  105.        
  106. --- !ruby/object:Allergy
  107. attributes:
  108.   id: 298486374
  109.   name: MyString
  110.   desc: MyText
  111.   created_at: 2012-01-09 14:28:21.000000000Z
  112.   updated_at: 2012-01-09 14:28:21.000000000Z
  113.   patient_id: 1
  114.   Patient Load (0.2ms)  SELECT "patients".* FROM "patients" WHERE "patients"."id" = 1 LIMIT 1
  115. --- !!null
  116. ...
  117.        
  118. allergy_one:
  119.   name: ...
  120.   patient: patient_one
  121.  
  122. allergy_two:
  123.   name: ...
  124.  
  125. patient_one:
  126.   name: ..