2012-01-09 12 views
0

私は自分のテストで何が間違っているのか把握しようとしています。 まず、コンソールで動作します。Ruby on Rails:関連モデル機能テストエラー

コンソール:

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

FYI:??私はRailsの3.1

答えて

3

上のRubyを使用していますあなたの備品が同じ名前を持っています次のように名前を付けます。

allergy_one: 
    name: ... 
    patient: patient_one 

allergy_two: 
    name: ... 

patient_one: 
    name: .. 

フィクスチャでpatient_idを使用しないことに注意してください。固定具がランダムなIDを使用するために必要な患者の治具を参照する必要があります。

関連する問題