2
我正在爲一個稱爲:addressable
的多態屬性的地址模型寫一個rspec。我正在使用工廠女孩進行測試。rspec validates_presence_of多態屬性ruby on rails
這個模型沒有控制器,因爲我不想創建一個獨立的地址,但是這並不能阻止rspec創建一個獨立的地址。我的模型,工廠和RSpec是
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
belongs_to :state
attr_accessible :street, :city, :state_id, :zip
validates_presence_of :street, :city, :zip, :state
validates_associated :state
end
Factory.define :address do |address|
address.street "1234 Any st"
address.city "Any City"
address.zip "90001"
address.association :state
end
describe Address do
before(:each) do
state = Factory(:state)
@attr = {:street => "1234 Any St", :city => "Any City", :zip => "Any Zip", :state_id => state.id}
end
it "should create a new address given valid attributes" do
Address.create!(@attr).valid?.should be_true
end
end
這RSpec的測試將創建addressable_id和NULL和NULL的addressable_type但如果我從任何其他模型創建,可以說,一個用戶模型或商店模式就會把一個地址在正確的數據。所以我試圖找出並研究失敗的是如何驗證多態屬性並通過工廠對其進行測試。
對不起,我很久沒有找到解決辦法,我開始考慮我可能會以錯誤的方式來解決這個問題。