Sto usando rspec e sto provando a verificare se il mio modello y ha molti x. Ho provato tutti i tipi di cose, incluso il ciclo attraverso l'array di metodi, e non riesco a trovare un buon metodo online. Quindi cosa dovrei usare?Come si esegue il test per belongs_to e has_many in Rails?
8
A
risposta
19
Senza molto l'hacking è possibile utilizzare eccezionale gioiello: http://github.com/carlosbrando/remarkable
Tratto da notevoli docs:
describe Post do
it { should belong_to(:user) }
it { should have_many(:comments) }
it { should have_and_belong_to_many(:tags) }
end
6
È possibile riflettere sulla classe:
MyModel.reflect_on_association(:x).macro == :has_one
E 'probabilmente più facile se si utilizza Shoulda, ci sono metodi di supporto in modo che legge molto più pulito: it { should have_many(:x) }
1
ecco una soluzione RSpec indipendente, il chiave è quella di utilizzare reflect_on_assocation
class MyModel < ActiveRecord::Base
has_many :children
belongs_to :owner
end
reflection_children = MyModel.reflect_on_association(:children)
if !reflection_children.nil?
if reflection_children.macro == :has_many
# everything is alright
else
# it's not has_many but exists
end
else
# it doesn't exist at all !
end
reflection_owner = MyModel.reflect_on_association(:owner)
if !reflection_owner.nil?
if reflection_owner.macro == :belongs_to
# everything is alright!
else
# it's not belongs_to but exists
end
else
# it doesn't exist at all!
end
Problemi correlati
- 1. Rails modello has_many, belongs_to relazioni
- 2. Rails: ordinare utilizzando una relazione has_many/belongs_to
- 3. Rails has_many e belongs_to rapporto con l'errore della chiave esterna
- 4. rails has_many: through has_many: through
- 5. Rails 3 costruire un tag select con has_many belongs_to associazione
- 6. Rails act_as_paranoid e has_many: through
- 7. Rails has_one vs belongs_to semantics
- 8. Rails 3.2.2 - has_many attraverso
- 9. Rails Polymorphic has_many
- 10. Come si esegue il test per NaN in JavaScript?
- 11. Differenza tra has_one e belongs_to in Rails?
- 12. Rails polymorphic has_many: through
- 13. Rails 3 Ordina per conteggio has_many: tramite
- 14. has_many build method, Rails
- 15. rails has_many: attraverso le associazioni
- 16. Rails: belongs_to vs has_one
- 17. ActiveAdmin personalizzare il modulo per belongs_to
- 18. Rails has_many ActiveRecord Association personalizzato
- 19. Rails RSpec Test per un has_many: attraverso Relationship
- 20. Quando viene attivato il tocco per un belongs_to in Rails?
- 21. In Rails, come posso recuperare l'oggetto su un'associazione belongs_to, senza passare per il database?
- 22. L'impostazione di una: has_many: attraverso l'associazione in un'associazione belongs_to Ruby on Rails
- 23. Rails has_many con nome alias
- 24. Come rettificare le versioni su has_many/belongs_to associazione con paper_trail
- 25. Impossibile trovare Inverse Associazione per has_many in Rails 3
- 26. Come si esegue il test dell'unità per EXC_BAD_ACCESS?
- 27. Convalida il numero di elementi has_many in Ruby on Rails
- 28. Come viene implementato il ruby on rails has_many (e simili)?
- 29. Ruby-on-Rails: più has_many: attraverso possibile?
- 30. Come si esegue il test dell'unità sui formati in Django?
Very nice! Penso che potrei iniziare a usare quella gemma. –
Un'altra opzione è Shoulda di thoughtbot, che ha una sintassi molto simile. http://github.com/thoughtbot/shoulda –
Un aspetto notevole è più completo di Shoulda. Non l'ho visto prima. –