How to do unit test whic allows null in the column

Hi, I have a carts table with columns: campsite, arrival_date, nights,
parents, children, family_claim, total. Here children and family_claim
column allows null so even if the value is not passed for these columns
it should be valid entry. How can I achieve this. At the moment I’m
using this way in the cart_test.rb:

def test_invalid_with_empty_attribute
cart = Cart.new(:children => “”, :family_claim => 1)
assert !cart.valid?
assert cart.errors.invalid?(:campsite)

end

So, I just wan’t to make sure I’m on the right track. Thanks.

Hi Jay,

I quite like this way of working its nice and clean and each test
demonstrates exactly what you expect to be invalid and why

def setup
@va.lid_attributes = {
:parents => 1
:children => 1,
:family_clam => 1,
:campsite => ‘blah’,
:total => 100,
}
end

def test_valid
cart = Cart.create(@valid_attributes)
assert cart.valid?
end

def test_valid_if_children_is_nil
cart = Cart.create(@valid_attributes.merge(:children => nil)
assert cart.valid?

or

assert cart.errors.on(:children).nil?
end

def test_invalid_if_total_is_nil
cart = Cart.create(@valid_attributes.merge(:total => nil)
assert ! cart.valid?
assert cart.errors.on(:total)
end

I hope this helps.

RobL

I hope this helps.

RobL

Thanks RobL. It sure did help…