2016-03-21 14 views
0

Raylarımda api birçok ilişkiye sahip birden fazla model var ve bunların her birini ayarlamayan bir şeyi kaçırmadığımı test etmenin bir yolunu istiyorum. Minitest'teki bir ilişkinin doğru kurulduğunu test etmenin en iyi yolu nedir? Görmek istediğim, makbuzun çok fazla satış yapabilmesi.Bir çok ilişki çalışmayı test edin

test 'multiple sales can have one receipt' do 
sale = Sale.new 
sale.receipt = 1234 
sale.save 

sale2 = Sale.new 
sale2.receipt = 1234 
sale2.save 


assert_equal sale.invoice_id, sale2.invoice_id, "receipts are not the same" 
end 

cevap

0

Böyle bir test olacağını testlerinizi yazabilirsiniz: Ben olacak

test 'one receipt can have multiple sales' do 
    receipt = Receipt.last 
    sale1 = receipt.sales.create 
    sale2 = receipt.sales.create 

    assert receipt.sales.size > 1, "receipt should have multiple sales" 
    assert_equal sale1.receipt, sale2.receipt, "receipts are not the same" 
end 
0

shoulda-matchers mücevher zaman git mücevher böyle test dernekler içindir. Dernekler için tüm olası senaryoları el ile test etme tediumunu kaydeder.

https://github.com/thoughtbot/shoulda

Sadece Gemfile için gem eklemek ve ... gibi

class UserTest < Test::Unit::TestCase 
    should have_many(:posts) 
end 
+0

Teşekkür Steve daha derinlere bak. Yine de bu projeye mücevher dosyaları eklememekle sınırlı olabilirim. Daha manuel bir yaklaşıma ihtiyaç olabilir ama bu testin güzel gözüktüğünü kabul ediyorum. –