鉴于我有以下课程
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