Próbuję przetestować powiązania w moich testach kontrolera Rspec. Problem polega na tym, że Factory nie tworzy asocjacji dla polecenia attributes_for. Tak więc, zgodnie z sugestią w this post I zdefiniowane atrybuty mój validate w moim kontrolera specyfikacji tak:FactoryGirl attributes_for and association
def valid_attributes
user = FactoryGirl.create(:user)
country = FactoryGirl.create(:country)
valid_attributes = FactoryGirl.build(:entitlement, user_id: user.id, country_id: country.id, client: true).attributes.symbolize_keys
puts valid_attributes
end
Jednak gdy Próby kontroler wciąż otrzymuję następujące błędy:
EntitlementsController PUT update with valid params assigns the requested entitlement as @entitlement
Failure/Error: entitlement = Entitlement.create! valid_attributes
ActiveRecord::RecordInvalid:
Validation failed: User can't be blank, Country can't be blank, Client & expert are both FALSE. Please specify either a client or expert relationship, not both
Jeszcze The wyjście valid_attributes w terminalu wyraźnie pokazuje, że każdy valid_attribute ma user_id, country_id i ekspert jest ustawiona na wartość true:
{:id=>nil, :user_id=>2, :country_id=>1, :client=>true, :expert=>false, :created_at=>nil, :updated_at=>nil}
Ach dzięki - pracowali nad tym zbyt długo tej nocy! –