2017-03-02 71 views

回答

0

假設有問題的型號被命名爲Car(你有沒有貼封裝類的,所以我完全做這件事的名字),你可以測試它:

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回報的元數據指定的關聯(類型ActiveRecord::Associations::BelongsToAssociation等)。返回的對象響應#loaded?,如果關聯已被加載,則返回true,否則返回false

+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'。 –