我一直想弄清楚我的测试有什么问题。 首先,它在控制台中工作。
控制台:
1.9.2-p290 :015 > a = Allergy.all
Allergy Load (0.4ms) SELECT "allergies".* FROM "allergies"
=> [#<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>]
1.9.2-p290 :016 > a[0]
=> #<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>
1.9.2-p290 :017 > a[0].patient.full_name
Patient Load (0.5ms) SELECT "patients".* FROM "patients" WHERE "patients"."id" = 1 LIMIT 1
=> "Test Full Name"
1.9.2-p290 :018 >
过敏控制器
class AllergiesController < ApplicationController
# GET /allergies
# GET /allergies.json
def index
@allergies = Allergy.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @allergies }
end
end
# GET /allergies/1
# GET /allergies/1.json
def show
@allergy = Allergy.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @allergy }
end
end
...
过敏模型
class Allergy < ActiveRecord::Base
validates :name, :presence => true
belongs_to :patient
end
患者模型
class Patient < ActiveRecord::Base
validates :first_name, :last_name, :dob, :presence => true
has_many :allergies
def age
now = Time.now.utc.to_date
now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
end
def full_name
first_name
end
end
测试:
require 'test_helper'
class AllergiesControllerTest < ActionController::TestCase
setup do
@allergy = allergies(:one)
@allergy.patient = patients(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:allergies)
end
...
灯具:
# For Allergy
one:
name: MyString
desc: MyText
patient_id: 1
two:
name: MyString
desc: MyText
patient_id: 1
#For Patient
one:
first_name: Jose
last_name: Rizal
middle_name: H
dob: 2009-10-29
two:
first_name: MyString
last_name: MyString
middle_name: MyString
dob: 1982-02-11
错误:
AllergiesControllerTest:
PASS should create allergy (0.22s)
PASS should destroy allergy (0.01s)
PASS should get edit (0.13s)
ERROR should get index (0.14s)
ActionView::Template::Error: undefined method `full_name' for nil:NilClass
/Users/wenbert/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/whiny_nil.rb:48:in `method_missing'
PASS should get new (0.02s)
PASS should show allergy (0.02s)
PASS should update allergy (0.02s)
我检查了日志,并且在运行测试时没有保存患者。我得到:
--- !ruby/object:Allergy
attributes:
id: 298486374
name: MyString
desc: MyText
created_at: 2012-01-09 14:28:21.000000000Z
updated_at: 2012-01-09 14:28:21.000000000Z
patient_id: 1
Patient Load (0.2ms) SELECT "patients".* FROM "patients" WHERE "patients"."id" = 1 LIMIT 1
--- !!null
...
那么,是什么给出了?
任何回复都将不胜感激。我现在已经坚持了将近4个小时。 :(
这可能是我的装置吗?我的模特?
最诚挚的问候,
w ^
仅供参考:我正在使用Ruby on Rails 3.1
答案 0 :(得分:3)
您的灯具具有相同的名称,将其命名为以下
allergy_one:
name: ...
patient: patient_one
allergy_two:
name: ...
patient_one:
name: ..
您会注意到我没有在灯具中使用patient_id。您需要引用您想要的患者固定装置,因为固定装置使用随机ID并且不从1开始。