j'ai un a beaucoup à travers le modèle de relation comme ceci:RSpec has_many à # <ActiveRecord :: Associations :: CollectionProxy>
class Physician < ApplicationRecord
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ApplicationRecord
belongs_to :physician
belongs_to :patient
end
class Patient < ApplicationRecord
has_many :appointments
has_many :physicians, through: :appointments
end
Je dois créer un médecin qui a de nombreux patients, non? ainsi de suite mon test:
let!(:physician) { create(:physician) }
let!(:patients) { create_list(:patients, 2) }
et je l'ai fait:
before { physician.patients << patients }
Je veux tester le JSON généré avec ce
expect(physician.as_json).to eq({
"id" => physician.id,
"name" => physician.name,
"patients" => physician.patients
})
Je m'y attendais passerait mais il a échoué à cause de cela #<ActiveRecord::Associations::CollectionProxy>
J'ai vérifié en utilisant binding.pry
par ceci:
physician.patients == patients
et le résultat a été true
Pourriez-vous me aider, que je manque quelque chose ici?
'médecin.patients' est une collection d'enregistrements. Ce n'est pas égal aux ** données json attendues **. Il y a plusieurs façons d'écrire ceci, mais fondamentalement votre test est faux - vous devez d'une manière ou d'une autre définir le ** format json ** que le résultat attendu devrait être. –
'# as_json' malgré le nom retourne un hash sérialisable - pas JSON. Sinon votre commentaire est sur @TomLord – max
merci @TomLord J'ai pu résoudre mon cas avec tous vos commentaires. – nrmb