2010-09-12 24 views

Trả lời

19

Nếu không có nhiều hack bạn có thể sử dụng đá quý đáng chú ý: http://github.com/carlosbrando/remarkable

Trích từ tài liệu đáng chú ý:

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! Tôi nghĩ rằng tôi có thể bắt đầu sử dụng đá quý đó. –

+3

Một tùy chọn khác là Shoulda bởi thinkbot, có cú pháp tương tự đáng kể. http://github.com/thoughtbot/shoulda –

+0

Vẻ đẹp đáng chú ý hoàn chỉnh hơn so với Shoulda. Tôi chưa từng thấy nó trước đây. –

6

Bạn có thể suy nghĩ về lớp:

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

Đó là lẽ dễ dàng hơn nếu bạn chỉ cần sử dụng shoulda, có những phương pháp helper để nó đọc nhiều sạch hơn: it { should have_many(:x) }

1

đây là một giải pháp độc lập rspec, các Điều quan trọng là phải sử dụng 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 
Các vấn đề liên quan