モデルTeacherLeader他のテーブルの外部キーを保持しています。私はこのテーブルを更新することができますし、私はrspecを介して私のコントローラのテストを書くことを試みます。Rails 3 - rspecによるPUTメソッドの正しいテスト
マイモデル:
# id :integer not null, primary key
# user_id :integer
# teacher_id :integer
#
class TeacherLeader < ActiveRecord::Base
belongs_to :user
belongs_to :teacher
validates :teacher_id,
:uniqueness => { :message => "already a class-head" }
end
私のコントローラだから、
class TeacherLeadersController < ApplicationController
def edit
@teacher_leader = TeacherLeader.find(params[:id])
...
end
def update
@teacher_leader = TeacherLeader.find(params[:id])
...
if (@teacher_leader.update_attributes(params[:teacher_leader]))
redirect_to teachers_path
...
else
redirect_to edit_teacher_leader_path
...
end
end
end
end
、私はPUTのための筆記テストに問題があります。ここでPUTメソッドのための私のコードの例です:
describe TeacherLeadersController do
render_views
before(:each) do
@teacher = Factory(:teacher)
@teacher.user.user_role = "teacher"
@teacher.save!
@user = Factory(:user, :user_login => Factory.next(:user_login))
@user.user_role = "class_head"
@user.save!
@sh = Factory(:user, :user_login => Factory.next(:user_login) )
@sh.user_role = "school_head"
@sh.save!
end
describe "PUT 'update" do
before(:each) do
@teacher_leader = @user.create_teacher_leader(@attr_teacher_leader)
end
describe "for signed-in school-heads" do
before(:each) do
test_sign_in(@sh)
end
it "should update teacher leader with correct params" do
put :update, :id => @teacher_leader, :teacher_id => @teacher.id
@teacher_leader.reload
@teacher_leader.teacher_id.should == @teacher.id
end
end
end
end
私は私がすることを得る私のスペックを実行しようとすると:
1) TeacherLeadersController PUT 'update for signed-in school-heads should update teacher leader with correct params
Failure/Error: put :update, :id => @teacher_leader, :teacher_id => @teacher.id
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/teacher_leaders_controller.rb:57:in `update'
# ./spec/controllers/teacher_leaders_controller_spec.rb:150:in `block (4 levels) in <top (required)>'
しかし、私のアプリケーションの動作を!テストで何が間違っているのですか?
「put:update、:id => @ teacher_leader.id、...」と表示されますか? – MikDiet