0

내 테스트는 다음과 같습니다이 테스트에서 각 개체를 만들어야합니까?

def setup 
    @period_registration= FactoryGirl.create(:period_registration) 
    end 


test "should post save_period" do 
    sign_in(FactoryGirl.create(:user)) 
    assert_difference('PeriodRegistration.count') do 
     post :save_period, period_registration: FactoryGirl.attributes_for(:period_registration) 
    end 
    assert_not_nil assigns(:period_registration) 

    end 

을하지만 그것을 실행할 때이 오류가 얻을 :

def save_period 
    @period_registration = PeriodRegistration.new(params[:registration]) 
    @period_registration.save 
    flash[:success] = "Successfully Registered for Session." 
    redirect_to event_url(@period_registration.period.event) 
    end 

내 공장은 다음과 같이 : 여기

1) Error: 
test_should_post_save_period(PeriodRegistrationsControllerTest): 
NoMethodError: undefined method `event' for nil:NilClass 

내 컨트롤러를 :

factory :event do 
    name 'First Event' 
    street '123 street' 
    city 'Chicago' 
    state 'Iowa' 
    date Date.today 
    end 


    factory :period do 
    name 'First Period' 
    description 'This is a description' 
    start_time Time.now + 10.days 
    end_time Time.now + 10.days + 2.hours 
    event 
    product 
    end 

factory :period_registration do 
    user 
    period 
    end 

기간 개체와 이벤트 개체를 만들어야합니까? 그렇다면 어떻게? 나는 이것이 문제라고 생각하지 않는다. 왜냐하면 나는 여러 공장에서 "기간"과 "제품"그리고 "이벤트"를 가지고 이들을 자동으로 생성한다고 믿기 때문이다.

여기에서 볼 수있는 아이디어가 있으십니까?

답변

1

짧은 대답 - 예, 개체를 만듭니다.

긴 대답 : 컨트롤러에서

  1. : 코드의

    @period_registration.period.event 
    

    이 줄은 The Law Of Demeter을 위반. 이것은 좋은 디자인이 아닙니다. 이 코드 행은 다음과 같습니다.

    @period_registration.event 
    

    그러나 PeriodRegistration 모델에서 새 메소드를 만들어야합니다. 방법의 간단한 변형은 다음과 같습니다 컨트롤러에서

    def event 
        period.event 
    end 
    
  2. : PeriodRegistration 모델은 저장하거나하지 않을 경우 당신은 확인하지 않습니다.

  3. PeriodRegistration 모델에는 2 개의 연관이 있고 FactoryGirl.attributes_for를 사용하면 팩토리가 관련 객체를 만들지 않고 PeriodRegistration에 대한 속성 세트 만 제공합니다. 이 테스트 합격을하기 위해서는 당신이 컨트롤러를 호출하기 전에이 두 객체를 생성해야합니다. 마찬가지로 가장 좋은 방법은 - 테스트는 단 하나의 주장 만 있어야합니다. 예를 들면 다음과 같습니다.

    def setup 
        @user = FactoryGirl.create(:user) 
        @period = FactoryGirl.create(:period) 
    end 
    
    test "should post save_period" do 
        sign_in(@user) 
        assert_difference('PeriodRegistration.count') do 
        post :save_period, period_registration: FactoryGirl.attributes_for(:period_registration, user: @user, period: @period) 
        end 
    end 
    
    test "should assings @period_registration" do 
        sign_in(@user) 
        post :save_period, period_registration: FactoryGirl.attributes_for(:period_registration, user: @user, period: @period) 
        assert_not_nil assigns(:period_registration) 
    end 
    
  4. 컨트롤러를 테스트 할 때 실제 모델 대신 모의 객체를 사용할 수 있습니다.

+0

# 1과 관련해서는 작동하지 않습니다. 나는 그것을 콘솔에서 테스트했고, 나는 이벤트를 얻기 위해 'pr.period.event'를 할 필요가있다. –

+0

귀하의 # 3은 제가 가지고있는 또 다른 문제에 대해 저를 도왔습니다! –

+0

@NoahClark 약 # 1. PeriodRegistration 모델에서 새 메서드를 만들어야합니다. 나는 나의 대답을 업데이트했다. –

관련 문제