2
저는 appointments_controller를 테스트하고 싶지만 약속을 생성하려면 사용자가 로그인해야합니다. cancancan을 사용합니다. 사용자가 자동으로 테스트 유닛에 로그인 할 수 있습니까? 여기 컨트롤러 테스트, 사용자는 모든 테스트 유닛에 로그인해야합니다.
는 가 load_and_authorize_resource에게class AppointmentsControllerTest < ActionDispatch::IntegrationTest
setup do
@heikoAppointment = appointments(:appointment_heiko)
end
test "should get index" do
get appointments_url
assert_response :success
end
test "should get new" do
sign_in users(:default)
get new_appointment_url(@heikoAppointment)
assert_response :success
end
을 ..... CanCanCan 을 포함 내 코드 테스트/컨트롤러/appointments_controller_test.rb입니다
컨트롤러/apointments_controller.rb
class AppointmentsController < ApplicationController
load_and_authorize_resource
layout 'appointment', only: [:shopper_show, :senior_show]
def index
@shopping_lists = ShoppingList.where(user_id: current_user.id)
@users = User.senior.where.not(id: current_user.id).order(:firstname)
#@appointments = Appointment.where(user_id: current_user.id)
#@invitations = Invitation.where(user_id: current_user.id)
#todo: merge @invitations.appointmen into @appointments
end
# GET /shopping_processes/1
# GET /shopping_processes/1.json
def show
@appointment = Appointment.find(params[:id])
@shopping_lists = get_both_lists
@users = get_both_users
end
# POST /shopping_processes
# POST /shopping_processes.json
def create
@appointment.status = nil
@appointment.processed = nil
@appointment.user_id = current_user.id
sl_created = create_shopping_list?
respond_to do |format|
if @appointment.save
format.html { redirect_to @appointment, notice: t(:appointment_created) }
format.json { render :show, status: :created, location: @appointment }
else
if sl_created
ShoppingList.find(@appointment.shopping_list_id).destroy
end
format.html { render :new }
format.json { render json: @appointment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /shopping_processes/1
# PATCH/PUT /shopping_processes/1.json
def update
respond_to do |format|
if @appointment.update(appointment_params)
format.html { redirect_to @appointment, notice: t(:appointment_updated) }
format.json { render :show, status: :ok, location: @appointment }
else
format.html { render :edit }
format.json { render json: @appointment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /shopping_processes/1
# DELETE /shopping_processes/1.json
def destroy
@appointment.destroy
respond_to do |format|
format.html { redirect_to appointments_url, notice: t(:appointment_deleted) }
format.json { head :no_content }
end
end
def shopper_index
end
def ready_index
appointments_hash = {}
customers = User.where.not(current_appointment: nil).where(role: :senior)
customers.each {|user| appointments_hash[user.current_appointment.id] = user.current_appointment}
shoppers = User.where.not(current_appointment: nil).where(role: :shopper)
shoppers.each {|user| appointments_hash.delete(user.current_appointment.id)}
@appointments = []
appointments_hash.each { |appointment_id, appointment| @appointments.push(appointment)}
end
def shopper_show
@appointment = Appointment.find(params[:id])
@shopping_lists = get_both_lists
both_users = get_both_users
@users = {}
first = true
both_users.each do |user|
@users[user.id] = {color: (first ? 'blue' : 'yellow'), name: user.firstname + ' ' + user.lastname}
first = false
end
end
def senior_show
@appointment = Appointment.find(params[:id])
if @appointment.user == current_user
@shopping_list = @appointment.shopping_list
else
@shopping_list = @appointment.invitation.shopping_list
end
#D.S.:Diese zuweisung funktioniert nicht richtig. Sie wurde vor den DB änderung erstellt und muss angepasst werden
#D.S.:ShoppingItem.joins(:list_item) und ListItem.where(shopping_list_id: @shopping_list.id]) ergeben ein korrektes Resultat
#D.S.:Aber zusammengenommen ist die query leer
#@shopping_items = ShoppingItem.joins(:list_item).where(list_item: [shopping_list_id: @shopping_list.id])
end
private def get_both_lists
shopping_lists = [ @appointment.shopping_list]
if @appointment.invitation && @appointment.invitation.shopping_list
shopping_lists << @appointment.invitation.shopping_list
end
shopping_lists
end
private def get_both_users
users = [ @appointment.user]
if @appointment.invitation
users.push(@appointment.invitation.user)
end
users
end
private
# Use callbacks to share common setup or constraints between actions.
def set_appointment
@appointment = Appointment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def appointment_params
params.require(:appointment).permit(:shopper_id, :status, :appointed, :processed, :shopping_list_id, invitation_attributes: [:user_id, :message ])
end
def create_shopping_list?
if @appointment.shopping_list_id.blank?
name = "Liste für den " + @appointment.appointed.strftime("%d.%m.%Y").to_s
invitee_id = "" + (@appointment.invitation.user_id.to_s) if @appointment.invitation
name = name + (" mit " + User.find(invitee_id).firstname) unless invitee_id.blank?
sl = current_user.shopping_lists.create(:name => name)
@appointment.shopping_list_id = sl.id
true
else
false
end
end
end
은 그 때 나는 test_should_get_new에 다음과 같은 오류가 발생합니다 :
NameError: undefined local variable or method `load_and_authorize_resource' for AppointmentsControllerTest:Class
나는, 부하를 알고 자원의 방법을 찾을 수 없습니다 권한을 부여합니다. 테스트 컨트롤러에 login-function을 작성해야 할 필요가 있을까요? 이 문제를 어떻게 풀려고합니까? 나는 무엇을 시도 할 수 있 었는가? 감사합니다
는 결론을 선도하고 무엇'고안 :: 시험 :: ControllerHelpers'가 관련이 있다는 데 도움이 it.Hope을 확인하고 있습니다? 테스트중인 컨트롤러를 게시 할 수 있습니까? 이것이 nil에 대한'env' 메소드 호출을 찾는 첫 번째 장소입니다. –
또한,'undefined method '외에도 nil에 대한 env'오류의 스택 추적은 무엇입니까? NilClass' - 행 번호가 있습니까? 테스트가 아니라면 자세한 플래그로 테스트를 실행하십시오. –
감사합니다. Devise :: Test :: ControllerHelpers를 제거했습니다. 나는 허가를 위해 칸칸칸을 사용합니다. 하지만 controller_test에서 이것을 어떻게 사용할 수 있습니까? – Peter