2010-09-12 15 views

Odpowiedz

19

Bez dużo włamań można użyć niezwykły klejnot: http://github.com/carlosbrando/remarkable

Zrobione z niezwykłych docs:

describe Post do 
     it { should belong_to(:user) } 
     it { should have_many(:comments) } 
     it { should have_and_belong_to_many(:tags) } 
    end 
+0

Very nice! Myślę, że mogę zacząć używać tego klejnotu. –

+3

Inną opcją jest Shoulda by thoughtbot, która ma niezwykle podobną składnię. http://github.com/thoughtbot/shoulda –

+0

Remarkable wygląda bardziej kompletnie niż Shoulda. Nie widziałem tego wcześniej. –

6

Można zastanowić się nad Klasa:

MyModel.reflect_on_association(:x).macro == :has_one 

to chyba łatwiej, jeśli po prostu użyć shoulda istnieją metody pomocnicze tak czyta o wiele bardziej czysty: it { should have_many(:x) }

1

oto rspec niezależne rozwiązania, Najważniejsze jest, aby używać 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 
Powiązane problemy