2017-03-02 9 views
2

내 모델에이 코드가 있습니다. Rspec 테스트 belongs_to with shoulda_matchers

belongs_to :user, -> { includes :user_profile } 

나는 내가 그 proc 포함 includes 부분을 테스트 할 수있는 방법을 발견하지 않았다 shoulda doc 통해 갔다. 도와주세요.

describe 'user' do 
    let(:car) { create_car_somehow } 

    it 'preloads the user_profile association' do 
    expect(car.user.association(:user_profile)).to be_loaded 
    end 
end 

#association 반환의 메타 데이터 :

+0

AFAIK –

답변

0

은 문제의 모델은 당신으로 테스트 할 수 있습니다 (그렇게 나는 완전히까지이 만들고있어 바깥 쪽 클래스의 이름을 게시하지 않은) Car이라는 가정하면 지정된 연결 (유형이 ActiveRecord::Associations::BelongsToAssociation 등). 반환 된 객체는 연관이로드 된 경우 true을 반환하고 그렇지 않으면 false을 반환하는 #loaded?에 응답합니다.

+1

어디에 be_loaded가 정의되어 있습니까? 문서에 대한 링크를 게시 할 수 있습니까? – Anthony

+1

즉석에서 정의 된 동적 [predicate matcher] (https://www.relishapp.com/rspec/rspec-expectations/v/2-2/docs/matchers/predicate-matchers)입니다. 기본적으로'expect (object) .to be_foo'는'expect (object.foo?). be_truthy'로 자동 변환됩니다. –