鉴于我有下面的类有没有办法阻止在测试中指定关联?
class listing > ActiveRecord::Base
attr_accessible :address
belongs_to :owner
validates :owner_id, presence: true
validates :address, presence: true
end
有没有一种方法可以让我摆脱不必保持之前,我保存在我的测试的列表中/spec/models/listing_spec.rb
关联的所有者,而不会使owner_id
通过质量分配访问?
describe Listing do
before(:each) do
@owner = Factory :owner
@valid_attr = {
address: 'An address',
}
end
it "should create a new instance given valid attributes" do
listing = Listing.new @valid_attr
listing.owner = @owner
listing.save!
end
it "should require an address" do
listing = Listing.new @valid_attr.merge(:address => "")
listing.owner = @owner
listing.should_not be_valid
end
end